Fill in a struct from file

This is a discussion on Fill in a struct from file within the C Programming forums, part of the General Programming Boards category; What would be the best way to fill in a struct with data from a file? Here are my two ...

  1. #1
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198

    Fill in a struct from file

    What would be the best way to fill in a struct with data from a file?

    Here are my two main proplems:

    1) How to parse the data out of the file?

    It will be in this format:

    Code:
    <attribute> <whitespace> = <whitespace> <value>
    For example:

    Code:
    myvalue = 12
    thing = somestuff
    2) Where to store the strings? Should I malloc() them or what? Is there a nicer way?

    Also I would make a function that would create a file from the same struct, but this should be really easy using fprintf().

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There is no "best" way, there are just ways that work well, and ways that don't (or are just silly like getting one char at a time from the file).

    If the data is firmly fixed in it's format, then fscanf() works well. fgets() is usually a good choice, as well.

    Strings to hold the data can be in a static char *array[], (using a pointer and a char array), a full bore 2D char array[][], a struct with an array of structs, a linked list with each node holding strings and values, or dynamic arrays similar to the static arrays mentioned, above.

    Which is best depends on things like:

    1) How many variables are you getting, and how big are they?

    2) On each run of the program, do you expect roughly the same number of values?

    3) Could the program be arranged so it only needed some of the data, instead of all of it, at any one time?

    4) Would making records with fields, be worthwhile?

    Things like that, can help make the decision to go one way, or another.

  3. #3
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Here's a better example:

    PHP Code:
    struct datafile {
        
    char *str1;
        
    char *str2;
        
    int number;
        
    linkedlist *list;
    }

    write_datafile(struct *datafilechar *filename)
    {
        
    FILE *fp fopen(filename"w");

        
    fprintf(fp"str1 %s\n"datafile->str1);
        
    fprintf(fp"str2 %s\n"datafile->str2);
        
    fprintf(fp"number %d\n"datafile->number);

        
    linkedlist *iter datafile->list;
        while (
    iter) {
            
    fprintf(fp"item = %s"iter->data);
            
    iter iter->next;
        }

    The format of the file will be the name of the field, any amount of whitespace chars, and then everything from the first non-whitespace char to the end of the line is the value.

    An example file for the above program:

    Code:
    str1                a string
    str2    another longer string
    item these
    item are
    item list
    number  32
    item items

  4. #4
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    I guess that malloc() is the only reasonable way, as far as I can think of.

    Another idea might be to use fixed-size char arrays in the structs, but that means that there will be a needless arbitrary limit on the length of string, and that the struct will use more memory because of all the unused array elements.

    http://catb.org/jargon/html/C/C-Programmers-Disease.htmlk

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    str1                a string
    str2    another longer string
    item these
    item are
    item list
    number  32
    item items
    I would use fgets() and bring the entire line, into a char buff[90]. (You can see evidence of the C programmer's disease, but VERY few lines with two or three strings exceed 89 char's).

    Then I'd use a do while loop to "pick" out the strings from within the buffer, to put them into a data structure.

    so:

    File chars >> buff[] with fgets >> final struct with sscanf() >> ready for next line in the file.

    Note that fgets needs 1 char of space for the '\0' (end of string char), which it will want to append onto the end of each line. (not each string, each line). Also, there will be a newline at the end of the line, and fgets() keeps those - so there are TWO char's you don't see, at the end of each line.

    Put up a bit of code and we'll steer you straight on using the above. The worst thing you can do now, is to keep changing how you want to do it - we can't do anything constructive to help you make that decision. There are no statistics on what input from a file method that new C programmers prefer, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  4. Replies: 10
    Last Post: 05-19-2006, 12:23 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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