Help with file I/O

This is a discussion on Help with file I/O within the C Programming forums, part of the General Programming Boards category; Hi all; I'm trying to figure out how to read the contents of a text file and put them into ...

  1. #1
    Registered User cantore's Avatar
    Join Date
    Feb 2006
    Posts
    8

    Post Help with file I/O

    Hi all; I'm trying to figure out how to read the contents of a text file and put them into a struct.
    For example, the text file that I have contains this:

    91.4, 1.76, Mike Jones
    87.5, 1.84, John Marshall
    88.0, 1.75, Ryan Musi

    .....and so on...
    the text file goes on with more names (the numbers in the first two columns represent the weight and height).
    My question is how can I put that data into an array of structs so I can later use it for other calculations.
    The struct will look something like this:

    typedef struct
    {
    double weight;
    double height;
    char name[50];
    } person;
    then I will create an array of structs for each person but right now my 'problem' is getting the weight, height and name and putting them into a struct.
    Any help, idea, suggestion will be appreciated. Thanks in advance.

  2. #2
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    fgets/sscanf
    Code:
          char line[80];
          person p[10];
          size_t size, i = 0;
          while ( fgets(line, sizeof line, file) != NULL )
          {
             if ( sscanf(line, "%lf, %lf, %49[^\n]", 
                         &p[i].height, &p[i].weight, p[i].name) == 3 )
             {
                if ( ++i >= sizeof p / sizeof *p )
                {
                   break;
                }
             }
          }
    Last edited by Dave_Sinkula; 04-02-2006 at 12:45 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User cantore's Avatar
    Join Date
    Feb 2006
    Posts
    8
    thanks, I'll try that out

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    hi Dave_Sinkula i got question here..what does this code of ur's mean
    Code:
    if ( ++i >= sizeof p / sizeof *p )
                {
                   break;
                }

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    If the result of incrementing i would be outside the range for indexing into the array p, stop now. Since the array name is in scope, sizeof p / sizeof *p is the number of elements in the array.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-31-2004, 12:21 AM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 09:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM

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