Thread: reading struct from file

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    9

    reading struct from file

    Hello,
    I am trying to read a file and input it to a structure
    So far I have been able to input first three items
    but to input the last item (name) i need to use fgets? becasue the name contains whitespaces
    how can I go about this?

    Thanks
    Sean

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct
    {
    	int registration_number;
    	int year;
    	char degree_scheme[20];
    	char name[30];
    
    }rec;
    
    int readRec(FILE *fp, rec *record);
    
    int main(void)
    {
    
        FILE *f;
        int i;
    
        rec recs[10];
    
        f = fopen ("students.txt", "r" );
    
        if ( f == NULL ){
    	printf("Cannot open record file");
    	exit(1);
        }
    
        for (i = 0; i < 10 && readRec(f, &recs[i]); ++i)
        {
        	printf("%d %d %s %s\n", recs[i].registration_number, recs[i].year, recs[i].degree_scheme, recs[i].name);
        }
    
        return 0;
    }
    int readRec(FILE *fp, rec *record)
    {
        return fscanf(fp, "%d %d %s", &record->registration_number, &record->year, record->degree_scheme) == 3;
    }
    sample data

    Code:
    32553 1 PCS dsgasd sdagasd
    32153 2 JHM asdgag sgsd
    34662 3 PCS asdg sdgas
    36446 1 JHM sgdsga gasdg
    23325 3 JHM gdsagasd gasdg
    23532 2 JHM asdgsa gsdgsa

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't see why you can't fscanf() two numbers and three strings. If the name has first name and last name to it, and you want to combine them, then add a blank space to the first name, and strcat() the second name, right behind that space.



    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
      int i;
      char fname[20];
      char lname[20];
      printf("\n\n Enter a First Name: ");
      scanf("%s", fname);
      i = strlen(fname);
      fname[i] = ' ';
      fname[i+1] = '\0';
      printf("\n\n Enter a Last Name: ");
      scanf("%s", lname);
      strcat(fname, lname);
      printf("fname: %s   lname: %s", fname, lname);
    
      i = getchar();
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    9
    The problem with that is if there is a middle name or more than 1 middle name, it wont work
    is there any way to implement a fgets()?
    I still cant figure out how to

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Try this:
    Code:
    return fscanf(fp, "%d %d %s %[^\n]29s", &record->registration_number, &record->year, record->degree_scheme, record->name) == 4;
    This:
    Code:
    %[^\n]29s
    tells fscanf to read up to 29 characters or until it hits a newline. The [] brackets contain the valid characters to read; in this case the use of the ^ -- the negation operator -- says any character is OK EXCEPT a newline.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    fgets() doesn't stop when it encounters a white space - that's fscanf(). fgets() continues until either it has reached the number of bytes you set it for, or it runs into a newline.

    Did you run into a problem using fgets() for the names?

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    9
    Code:
      
      return fscanf(fp, "%d %d %s %[^\n]29s", &record->registration_number, &record->year, record->degree_scheme, record->name) == 4;
    this works.
    although, if the name is blank -- the whole next record is inputted as name.
    but thats fine
    thanks a lot. saved a lot of time
    pc

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    9
    is there any way to ignore white spaces when using fscanf?

    Code:
    fscanf(f,"%s",a[i]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. having trouble reading into Struct from File
    By bcianfrocca in forum C++ Programming
    Replies: 9
    Last Post: 09-06-2005, 10:54 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM