Thread: Read from file into structure

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    8

    Read from file into structure

    Hello,

    When I tried to run my code I keep getting a "Segmentation fault". I don't know what I am doing wrong. Can someone help me?

    I am trying to write a code that read from a file and put the data into a structure.

    The file look like the following:
    2001,ABBIGAEL,5
    1994,ABBIGAIL,5
    1996,ABBIGAIL,8
    1997,ABBIGAIL,13

    The file have 31 lines.

    Here is my code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    
    
    int main() {
    /* Define a daydata structure */
            typedef struct {
                    int year; int freq;
                    char name[31];
                    } daydata ;
            daydata record[40];
            FILE * filehandle;
            char lyne[121];
    
    
            char *item;
            int reccount = 0;
            int k;
    
    
            /* Here comes the actions! */
            /* open file */
    
    
            filehandle = fopen("female.csv","r");
    
    
            /* Read file line by line */
    
    
            while (fgets(lyne,120,filehandle)) {
                    printf("%s",lyne);
    
    
                    item = strtok(lyne,",");
                    record[reccount].year= atoi(item);
    
    
                    item = strtok(NULL,",");
                    strcpy(record[reccount].name,item);
    
    
                    item = strtok(NULL,"\n");
                    record[reccount].freq= atoi(item);
    
    
                    printf("%d\n",record[reccount].year);
                    reccount++;
                    }
    
    
            /* Close file */
    
    
            fclose(filehandle);
    
    
            /* Loop through and report on data */
    
    
            printf("Name Record\n");
            for (k=0; k<reccount; k++) {
                    printf("It is %s\n",record[k].name);
                    }
    }
    OUTOUT:


    2001,ABBIGAEL,5
    2001
    1994,ABBIGAIL,5
    1994
    1996,ABBIGAIL,8
    1996
    1997,ABBIGAIL,13
    1997
    1998,ABBIGAIL,18
    1998
    1999,ABBIGAIL,15
    1999
    2000,ABBIGAIL,13
    2000
    2001,ABBIGAIL,17
    2001
    2002,ABBIGAIL,23
    2002
    2003,ABBIGAIL,20
    2003
    2004,ABBIGAIL,17
    2004
    2005,ABBIGAIL,36
    2005
    2006,ABBIGAIL,26
    2006
    2007,ABBIGAIL,23
    2007
    2008,ABBIGAIL,34
    2008


    Segmentation fault

    Thank you.
    Last edited by Phuoc Phan; 11-24-2013 at 02:35 PM. Reason: Missing something

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Is there a blank line at the end of the file? The output suggests that possibility, and that would cause you to dereference a NULL pointer or two from strtok.

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    8
    Yes, there was a blank line at end of file.Its working fine now. Thank you.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Have you considered scanf for reading your line rather than strtok. Since each line is a fixed format, this would also give you error checking ability:

    Code:
    /*...*/
        int year;
        char name[1000];
        int freq;
        char dummy;
        
        if (sscanf(line, " %d, %999[^,], %d %c", &year, name, &freq, &dummy) != 3) {
            fprintf(stderr, "Invalid input. Bye.\n");
            return 1;
        }
    
    /*...*/

  5. #5
    Registered User
    Join Date
    Sep 2013
    Location
    Florida
    Posts
    14
    Quote Originally Posted by c99tutorial View Post
    Have you considered scanf for reading your line rather than strtok. Since each line is a fixed format, this would also give you error checking ability:

    Code:
    /*...*/
        int year;
        char name[1000];
        int freq;
        char dummy;
        
        if (sscanf(line, " %d, %999[^,], %d %c", &year, name, &freq, &dummy) != 3) {
            fprintf(stderr, "Invalid input. Bye.\n");
            return 1;
        }
    
    /*...*/
    Or better yet, use fscanf to replace both fgets and strtok and that kills two birds with one stone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems saving read file data into a structure
    By jcmoney in forum C Programming
    Replies: 3
    Last Post: 11-19-2012, 05:37 AM
  2. Read values from txt file and store them in structure
    By bojomojo in forum C Programming
    Replies: 6
    Last Post: 12-19-2010, 10:36 AM
  3. Replies: 5
    Last Post: 03-18-2006, 11:25 AM
  4. read a structure from a file
    By spank in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 12:23 PM
  5. How to read from a file into a structure?
    By aspand in forum C Programming
    Replies: 3
    Last Post: 05-28-2002, 11:56 AM

Tags for this Thread