C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-20-2008, 11:19 AM   #16
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,368
Quote:
it tells errors as i change printf(too few arguments)
... one must be a master in mind reading to give answers here==(

You probably forgot the FILE argument when using fprintf(), but I lack the mind reading skills to be sure in the absence of the code.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 09-20-2008, 11:45 AM   #17
Registered User
 
Join Date: May 2008
Posts: 70
where should i insert it
overlord21 is offline   Reply With Quote
Old 09-20-2008, 11:46 AM   #18
Registered User
 
Join Date: May 2008
Posts: 70
exuse for lack of understanding
overlord21 is offline   Reply With Quote
Old 09-20-2008, 11:50 AM   #19
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,368
Quote:
where should i insert it
If you have to ask that, then you have no clue as to what your program is doing.

Quote:
exuse for lack of understanding
It's okay, but let's get you started on file handling. Put aside your current work for the time being and write a program that reads in some text of no more than 14 characters from the user and writes the text to a file (pick a filename of your choice) using fprintf().
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 09-20-2008, 12:10 PM   #20
Registered User
 
Join Date: May 2008
Posts: 70
i'll just let it for now it just that the instructor did only the lectur with no exemple of an implementation using fprintf and stuff. I'll make my mind later
overlord21 is offline   Reply With Quote
Old 09-20-2008, 01:38 PM   #21
Registered User
 
Join Date: Aug 2008
Posts: 67
http://www.cppreference.com/wiki/c/io/fopen
http://www.cppreference.com/wiki/c/io/fprintf
http://www.cppreference.com/wiki/c/io/fclose
kpreston is offline   Reply With Quote
Old 09-20-2008, 02:05 PM   #22
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
The bottom line is you need to have all of the relevent API documentation handy before you start coding. And a little initiative wouldn't hurt either...
Sebastiani is offline   Reply With Quote
Old 09-24-2008, 07:36 AM   #23
Registered User
 
Join Date: May 2008
Posts: 70
thanks for the useful links. So the result that i got is this
Code:
#include<stdio.h>
#include<string.h>
#define size 5           


typedef struct {
	char last[10];                        /* Structure of type student*/
	char first[10];              
    double gpa;
} stud;


typedef struct {
	char last[10];
	char first[10];                      /* structure of type employee*/
	double salary;
}  emp;


void check_add_salary (emp *e, stud *s);




main(){ 

FILE *stud_file=NULL;
FILE *employ_file=NULL;
FILE *employ_file2=NULL;
int i=0 , j=0;
stud arr_stud[size];                                                  /* An array of structures (students)*/
emp arr_emp[size];                                                    /*An array of structures (employees)*/
                

if((stud_file=fopen("student.txt","r"))==NULL) 
   printf("\nThe file could not be opened\n\n");

else
   {   fseek(stud_file,50,SEEK_SET);
       while(!feof(stud_file))
   	   {fscanf(stud_file,"%s%s%lf\n", arr_stud[i].last,arr_stud[i].first,&arr_stud[i].gpa);
   
       i++;
   	   }
   
       
       
     
     
      fclose(stud_file);
   }
   /*end of else*/
   
   i=0;
   
   if((employ_file=fopen("employees.txt","r"))==NULL)
{  
   printf("\nThe file could not be opened\n\n");
}

else 
   {
     fseek(employ_file,51,SEEK_SET);
   	 while(!feof(stud_file))
	   	{
   	    fscanf(employ_file,"%s%s%lf\n", arr_emp[i].last,arr_emp[i].first,&arr_emp[i].salary);
 	   
        i++;   
        }
   
   fclose(employ_file);
   
   
   
   
   }
   
   /***end of reading from file employess*/
   
   
   check_salary(arr_emp,arr_stud);
   
   
   
   if((employ_file2=fopen("update.txt","w"))==NULL)
{  
   printf("\nThe file could not be opened\n\n");
}

else 
   {  fputs("\t\tThe new list of employees(after salary change)\n\n",employ_file2);
       fputs("Last name\tFirst name\t\tGPA\n",employ_file2);
       fputs("************************************************\n",employ_file2);
       
       for(i=0;i<size;i++)
         fprintf(employ_file2,"%-10s      %-10s               %-10.2f\n", arr_emp[i].last,arr_emp[i].first,arr_emp[i].salary);   /* Display the list of employees sorted by alphabetical order*/
	   
       fputs("\n************************************************\n\n\n\n",employ_file2);
       fclose(employ_file2);
   
   
   
  
  printf("\n The file of employees with the new salary has been created n\" );
   


   
}
   void check_salary (emp *r, stud *s)
   	{
   	
   		int i , j;
        for(i=0;i<size;i++)
          for(j=0;j<size;j++)
            if( strcmp (e[i].last,s[j].last)==0 && strcmp(e[i].first,s[j].first)==0 )
              if(s[i].gpa>3.0)                                                                                             /* The if statement is to add 10% to the salary*/
                {																														/*of employees whose students have GPA>3*/
		        	r[i].salary = r[i].salary + ((r[i].salary)*0.1); j=size-1;
		     	}       
			
   }
but by using fseek its no more sequential . Anyone can tell how to make sequential?

Thanks
overlord21 is offline   Reply With Quote
Old 09-24-2008, 08:05 AM   #24
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by overlord21 View Post
thanks for the useful links. So the result that i got is this but by using fseek its no more sequential . Anyone can tell how to make sequential?

Thanks
Don't use fseek? It would seem to be wrong in this context.
tabstop is offline   Reply With Quote
Old 09-24-2008, 08:05 AM   #25
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,135
I think you are confused too much with the sequential thing. The difference is only that in a sequential format you don't have a file pointer that can take any value. You start from the beginning reading until you go to the end. That is what you are doing. Most of the times that is what people do.
You ONLY fseek() once, do go 50 spaces ahead. That is not random access. If you want, you can read 50 characters and have the same result with 50 times fgetc() which reads one character a time. That will move the file pointer 50 spaces ahead.

EDIT: I believe the difference with sequential and random access lies on the hardware, not the software. If you had a tape you will have to read from the beginning to the end. In a HD you can read from wherever you want. If you kind of think though how a tape reader would work, it would be fine to use fseek() if the tape reader wasn't too simple. If the tape reader didn't want to use the same functions with files then it would have its own, thus no fseek(). Anyway. Sequential is really not going back to the file. Thus not using fseek() to go back. But in any case, yeah, just not use fseek

Last edited by C_ntua; 09-24-2008 at 08:16 AM.
C_ntua is offline   Reply With Quote
Old 09-24-2008, 08:21 AM   #26
Registered User
 
Join Date: May 2008
Posts: 70
i see so the program is fine as it is now right ot should i remove one fseek since i use two?...... and for the " printf("\n The file of employees with the new salary has been created\n\n\");" it tells me that there is some unterminated string or character ..
overlord21 is offline   Reply With Quote
Old 09-24-2008, 08:35 AM   #27
Registered User
 
Join Date: May 2008
Posts: 70
Quote:
Originally Posted by C_ntua View Post
EDIT: I believe the difference with sequential and random access lies on the hardware, not the software. If you had a tape you will have to read from the beginning to the end. In a HD you can read from wherever you want. If you kind of think though how a tape reader would work, it would be fine to use fseek() if the tape reader wasn't too simple. If the tape reader didn't want to use the same functions with files then it would have its own, thus no fseek(). Anyway. Sequential is really not going back to the file. Thus not using fseek() to go back. But in any case, yeah, just not use fseek
wow with this i'm more confused,ok then if i don't use fseek and want to skip reading the first line of the line how should i do it?
overlord21 is offline   Reply With Quote
Old 09-24-2008, 08:55 AM   #28
Registered User
 
Join Date: Sep 2006
Posts: 2,517
Quote:
Originally Posted by overlord21 View Post
aie! it tells errors as i change printf(too few arguments)
Code:
Example:
#include <stdio.h>

int main(void)
{
   FILE *stream;
   int i = 100;
   char c = 'C';
   float f = 1.234;

   /* open a file for update */
   stream = fopen("DUMMY.FIL", "w+");

   /* write some data to the file */
   fprintf(stream, "%d %c %f", i, c, f);

   /* close the file */
   fclose(stream);
   return 0;
}
A simple example.
Adak is offline   Reply With Quote
Old 09-24-2008, 09:07 AM   #29
Registered User
 
Join Date: May 2008
Posts: 70
thanks and for the little problem in my last post can you help? Adak?
overlord21 is offline   Reply With Quote
Old 09-24-2008, 09:43 AM   #30
Registered User
 
Join Date: May 2008
Posts: 70
well i removed the fseek and it works
overlord21 is offline   Reply With Quote
Reply

Tags
files, program impleùmentation, random, sequential

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can we have vector of vector? ketu1 C++ Programming 24 01-03-2008 05:02 AM
struct question caduardo21 Windows Programming 5 01-31-2005 04:49 PM
Simple File encryption caroundw5h C Programming 2 10-13-2004 10:51 PM
what does this mean to you? pkananen C++ Programming 8 02-04-2002 03:58 PM
Outputting String arrays in windows Xterria Game Programming 11 11-13-2001 07:35 PM


All times are GMT -6. The time now is 02:02 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22