Thread: Read lines from a file and save them in a struct, help!

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    8

    Read lines from a file and save them in a struct, help!

    Hello!

    I need to read text from a file that is formatted like this:

    artist;album;release type;rating

    and then save it to a struct that looks like this:

    Code:
    typedef struct {
            char artist[41];
            char album[41];
            char type[3];
            char rating[5];
    } record;
    The problem is that I don't really now if I want to use fscanf or fgets and how to put the info in the struct. I also want to put the struct to a list but that's easier I think.

    Anyway here is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "dlist.h"
    #define BUFSIZE 90
    
    
    typedef struct {
            char artist[41];
            char album[41];
            char type[3];
            char rating[5];
    } record;
    
    
    void readFile(FILE *infilep, dlist *list);
    
    
    int main(int argc, char *argv[]) {
    
    
            
            FILE * infilep;
            infilep = fopen(argv[1],"r");
    
    
            if (infilep == NULL) {
                    fprintf(stderr, "Couldn't open input file.");
                    exit(1);
            }
    
    
            if(argc < 1) {
                    fprintf(stderr, "Usage: %s <musik.txt>\n", argv[0]);
                    exit(1);
            }
    
    
    
    
            
            dlist *list=dlist_empty();
            dlist_setMemHandler(list, free);
    
    
            
            readFile(infilep, list);
    
    
            return 0;
    }
    
    void readFile(FILE *infilep, dlist *list) {
    
    
            char line[BUFSIZE];
            dlist_position pos;
            pos = dlist_first(list);
    
    
       
            while (fgets(line, BUFSIZE, infilep) =! NULL) {
                    record *info=malloc(sizeof(record));
                    fgets(infilep, "%41[^;]%41[^;]%3[^;]%5[^;]",
                           &info->artist, &info->album,
                           &info->type, &info->rating);
                    dlist_insert(list, pos, record);
                    dlist_next(list, pos);
            }
    }

    Help appreciated!

    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
                    fgets(infilep, "%41[^;]%41[^;]%3[^;]%5[^;]",
                           &info->artist, &info->album,
                           &info->type, &info->rating);
    fgets doesn't work like that. Try fgets to read a line, then sscanf to parse it and put it into the appropriate fields. You could just use fscanf. Either way is fine really. But you should really just work on how to read into a record first before you worry about making lists.


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

  3. #3
    Registered User
    Join Date
    Nov 2009
    Location
    Maryland, USA
    Posts
    46
    Instead of that second fgets() call, I think you meant this, which I think should work:
    Code:
        if (info == NULL)
            { /* error! */ }
        memset(record, 0, sizeof(record)); /* In case of missing fields */
        sscanf(line, "%41[^;]%41[^;]%3[^;]%5[^;]",
               &info->artist, &info->album, &info->type, &info->rating);
    Also, in main(), you should check "(argc < 1)" before trying to open the file. Otherwise you could be passing a NULL pointer to fopen(), which might crash.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    8
    Quote Originally Posted by KenJackson View Post
    Instead of that second fgets() call, I think you meant this, which I think should work:
    Code:
        if (info == NULL)
            { /* error! */ }
        memset(record, 0, sizeof(record)); /* In case of missing fields */
        sscanf(line, "%41[^;]%41[^;]%3[^;]%5[^;]",
               &info->artist, &info->album, &info->type, &info->rating);
    Also, in main(), you should check "(argc < 1)" before trying to open the file. Otherwise you could be passing a NULL pointer to fopen(), which might crash.
    Thanks alot guys. I kind of understand how fgets and sscanf works now. But when I try to compile gcc says

    "audiofil.c: In function `readFile':audiofil.c:72: error: invalid lvalue in assignment
    audiofil.c:76: warning: char format, different type arg (arg 3)
    audiofil.c:76: warning: char format, different type arg (arg 4)
    audiofil.c:76: warning: char format, different type arg (arg 5)
    audiofil.c:76: warning: char format, different type arg (arg 6)
    audiofil.c:77: error: syntax error before "record""

    What does that mean?

    readFile looks like this now (I'll add memset and stuff later):

    Code:
    void readFile(FILE *infilep, dlist *list) {
    
    
            char line[BUFSIZE];
            dlist_position pos;
            pos=dlist_first(list);
    
            while (fgets(line, BUFSIZE, infilep) =! NULL) {
                    record *info=malloc(sizeof(record));
                    sscanf(line, "%41[^;]%41[^;]%3[^;]%5[^;]",
                           &info->artist, &info->album,
                           &info->type, &info->rating);
                    dlist_insert(list,pos,record);
                    dlist_next(list,pos);
            }
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Since info->artist is an array, you just need to pass it as an argument and it will be converted to a pointer to its first element. The same goes for the other char array members, i.e.,
    Code:
                    sscanf(line, "%41[^;]%41[^;]%3[^;]%5[^;]",
                           info->artist, info->album,
                           info->type, info->rating);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    8
    Quote Originally Posted by laserlight View Post
    Since info->artist is an array, you just need to pass it as an argument and it will be converted to a pointer to its first element. The same goes for the other char array members, i.e.,
    Code:
                    sscanf(line, "%41[^;]%41[^;]%3[^;]%5[^;]",
                           info->artist, info->album,
                           info->type, info->rating);
    Great! It works like a charm. But gcc still complains about one last thing, "error: invalid lvalue in assignment" at this line:

    Code:
    while (fgets(line, BUFSIZE, infilep) =! NULL)
    And I really can't see what the problem is...

    Thanks again for the great help!

    Edit: Aah... I just switched the "!=" with "=!"
    Last edited by mrtsk; 02-14-2012 at 06:48 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    =! is not !=
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. save struct in a txt file problem!
    By xcubis in forum C Programming
    Replies: 1
    Last Post: 05-30-2011, 02:01 PM
  2. Save data from two struct arrays in one .dat file
    By IndioDoido in forum C Programming
    Replies: 5
    Last Post: 03-27-2008, 03:50 PM
  3. Read lines from a file
    By steffi in forum C Programming
    Replies: 2
    Last Post: 11-13-2007, 06:05 AM
  4. Still can not write or read from save file
    By WackoWolf in forum C++ Programming
    Replies: 37
    Last Post: 11-14-2004, 07:28 PM
  5. Still can not write or read from save file
    By WackoWolf in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2004, 01:21 AM