Thread: how to read a file in c and store it in an structure?

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    1

    how to read a file in c and store it in an structure?

    Hi All,


    Can any one help me with the code how to read a text file and to save the contents of the file in a structure.

    My text file contains phone book directory with firstname, last name and phone number. Below is the format of my phone book.

    Ben John, 6212445732
    Ben Jessey, 852144521255

    can any one help me with a code ?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Break everything into smaller, more manageable functions. Here are my suggestions:
    Code:
    #include <stdint.h>
    
    struct Phone
    {
        char* firstName;
        char* lastName;
        uint64_t number;
    };
    
    // Reads new phone book directory and saves it into "*ptr". Returns false if there are no more entries
    bool readPhone(struct Phone* ptr, FILE* fptr);
    
    // --- IN MAIN ---
    
    struct Phone* book = NULL; // This holds all the structs
    struct Phone temp; // Temporary for reading
    
    while (readPhone(&temp, fptr)) {
        // Expand book by means of realloc
        // Assign temp to the last(not-used) index of the realloced array
    }
    
    // Profit!
    
    // Loop through all of them and call free for firstName and lastName. If you want, you could define a function to do that for you.
    Devoted my life to programming...

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    We are not here to write code for the OP, but to guide them, and comment on the code the OP writes.

    Phone numbers are better stored as text rather than as an unsigned integer.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by rstanley View Post
    We are not here to write code for the OP, but to guide them, and comment on the code the OP writes.
    To be fair, GReaper just posted some "guide" code snippets for the OP - though I suspect layers of memory allocation might be beyond the skill level of the OP. Fixed sized arrays might be more appropriate for purposes of this exercise.

    Quote Originally Posted by rstanley View Post
    Phone numbers are better stored as text rather than as an unsigned integer.
    Agreed.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by rstanley View Post
    Phone numbers are better stored as text rather than as an unsigned integer.
    I agree with this, but I thought that for a newcomer it'd be easier to have scanf do the testing, instead of having to check the string themselves for inappropriate characters.
    Devoted my life to programming...

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by GReaper View Post
    I agree with this, but I thought that for a newcomer it'd be easier to have scanf do the testing, instead of having to check the string themselves for inappropriate characters.
    The OP is obviously new to C. Best for the OP to learn reading and separating the data into a struct first, and deal with validation later.

    I would recommend reading in the line using fgets() then breaking up the line with either sscanf() or strtok().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-14-2011, 11:15 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. Read text file and store it in an array
    By look2hook in forum C Programming
    Replies: 2
    Last Post: 12-03-2010, 11:47 PM
  4. Read first line of a file and store in a variable
    By Saeid87 in forum C Programming
    Replies: 5
    Last Post: 06-19-2009, 11:27 AM
  5. Replies: 1
    Last Post: 03-27-2009, 04:21 AM

Tags for this Thread