Read A File From A Specific Line

This is a discussion on Read A File From A Specific Line within the C Programming forums, part of the General Programming Boards category; The object of my assignment is to read data from a file, capitalize the first letter of all the names ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    25

    Read A File From A Specific Line

    The object of my assignment is to read data from a file, capitalize the first letter of all the names within the file and rewrite the file. Simple enough right?

    Just one small problem. The file we have to use has a title. It looks like this:

    ID FIRSTNAME LASTNAME
    10 john doe
    20 mary jane

    30 jim smith
    This ID FIRSTNAME LASTNAME bit here messes up my file reading. I need to know how to read a file from whatever line I want. My code is as follows:

    Code:
    #include <stdio.h>
    struct employee
    {
    	char firstname[40];
    	char lastname[40];
    	int id;
    };
    typedef struct employee Employee;
    
    Employee e[3];
    
    void PrintEmployeeRecord();
    void SaveEmployeeRecord();
    //void Capitalize(const Employee e[]);
    
    int main ()
    {
    	int i=0, j =0, r=0, c=0;
    	Employee e[3];
    	
    	
    	FILE *file;	
    	
    	file = fopen("employee.dat", "r");// open file
    	
    	if(file ==NULL){
    		printf("Cannot create file");//initiate check
    		return;
    	}
    	
    	for (i=0; i<3; i++)
    	{
    		fscanf(file,"%d", &e[i].id);
    		fscanf(file,"%s", e[i].firstname);
    		fscanf(file,"%s", e[i].lastname);
    	}
    	
    	
    	fclose(file); //close file
    	
    	PrintEmployeeRecord();
    	
    	//Capitalize(e);
    	
    	PrintEmployeeRecord();
    	
    	SaveEmployeeRecord(e);
    }
    //END MAIN FUNCTION
    
    
    //purpose: Save employee record to file
    //input: array of 3 employee data
    //output: file with 3 employee data
    void SaveEmployeeRecord(const Employee e[])
    {
    	int i;
    	FILE *file;	
    	
    	file = fopen("employee.dat", "w"); // open file
    	
    	if(file ==NULL){
    		printf("Cannot create file");//initiate check
    		return;
    	}
    	
    	fprintf(file, "ID FIRSTNAME LASTNAME\n");  // print starter data
    	
    	for (i=0; i<3; i++)
    	{
    		fprintf(file, "%d %s %s \n", e[i].id, e[i].firstname, e[i].lastname); //print file data
    	}
    	printf("\n Written to file! \n");
    	
    	fclose(file);// close file
    }
    
    //purpose: Print Employee data
    //input: array of 3 employee data
    //output: 3 employee data printed to screen
    void PrintEmployeeRecord()
    {
    	int i;
    	for (i=0; i<3; i++);
    	{
    		printf("%d %s %s \n", e[i].id, e[i].firstname, e[i].lastname);
    	}
    }




    Since I haven't been able to properly read the file, I therefore haven't written my capitalize function (hence why it's commented out) but I suspect that I'll simply be using toupper(); in order to do this. (if this isn't the right way to go, please tell me)


    Can someone help me?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,577
    Use fgets to read the first line of the file, don't process it, then go into your read loop.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could do this a few ways. Probably the easiest is to just read every line with fgets, then check the first character that you've got stored and see if it is 'I'. If so, ignore what you have read, and read the next line. If it's a number, then use something like sscanf (similar to how you are using fscanf) to parse what you've read.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    25
    Quote Originally Posted by anduril462 View Post
    Use fgets to read the first line of the file, don't process it, then go into your read loop.
    I tried what you said and I had issues. I added the fgets before the loop and got the error "incompatible type for argument 1 of fgets"

    Code:
    	if(file ==NULL){
    		printf("Cannot create file");//initiate check
    		return;
    	}
    	
    	fgets(e[i], sizeof e[i], stdin);
    	for (i=0; i<3; i++)
    	{
    		fscanf(file,"%d", &e[i].id);
    		fscanf(file,"%s", e[i].firstname);
    		fscanf(file,"%s", e[i].lastname);
    	}

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,577
    Look at the definition of fgets:
    Quote Originally Posted by man page
    char *fgets(char *s, int size, FILE *stream);
    You need to pass it a char *, not an Employee object. You also don't want to read from stdin. You're reading that from the same file as the rest of your data:
    Code:
    char buf[256];
    
    fgets(buf, sizeof(buf), file);

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    25
    Quote Originally Posted by anduril462 View Post
    Look at the definition of fgets:


    You need to pass it a char *, not an Employee object. You also don't want to read from stdin. You're reading that from the same file as the rest of your data:
    Code:
    char buf[256];
    
    fgets(buf, sizeof(buf), file);


    This is what I have

    Code:
    	*fgets(char *e, sizeof(e), file);

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by dsured View Post
    This is what I have

    Code:
    	*fgets(char *e, sizeof(e), file);
    You can't inline declare e there to be a pointer to a character, and even if you could, you wouldn't have anything allocated for it.

    You need a buffer of some kind (an array of characters):
    Code:
    char mybuf[ SOMESIZEYOUPICK ];
    You pass that to it as the first argument, as shown above:
    Code:
    rval = fgets( mybuf, SOMESIZEYOUPICK, file );
    Now typically you want to pay attention to the return value of the function as well.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
      file = fopen("employee.dat", "r");// open file
      if(file ==NULL){
         printf("Cannot create file");//initiate check
         return;
       }
    
      int i = 0;
      int c = 0;
       do
         {  c = fscanf(file,"%d %s %s", &e[i].id, e[i].firstname, e[i].lastname);
                if (c == 3)  // all three matched the format
                  i++ ;
                    }
           } while ( c > EOF )  // end of file
    
       fclose(file); //close file
    Last edited by CommonTater; 03-16-2011 at 09:47 PM. Reason: Even Easier...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a specific line from a text file
    By acidrain in forum C Programming
    Replies: 3
    Last Post: 12-01-2009, 01:23 PM
  2. Replies: 7
    Last Post: 02-06-2009, 11:27 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. How do I read file line by line?
    By Blurr in forum C Programming
    Replies: 1
    Last Post: 09-22-2001, 12:32 AM

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