File I/O and structs

This is a discussion on File I/O and structs within the C Programming forums, part of the General Programming Boards category; Ok, so I have a file that lists information that I want to store in a struct. It is organized ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    11

    File I/O and structs

    Ok, so I have a file that lists information that I want to store in a struct. It is organized like this:

    [
    Label
    Data
    Designation

    (these three blank lines are purposely there)

    ]
    (
    Type-#
    Type-#
    ...
    )

    There will be an unknow number of these two different types of listings. They are all in one file, with all the [ blah blah] coming before the ( blah blah ).

    So, I want to read the file in, each line at a time, putting the results in a struct. My struct will have a place for the label, data, designation and another for the types and numbers. The numbers have to correspond with the types. How can I have my program run through the file and sort out the data?

    I assume fgets would be the best, but how do i stop it from going past the \n and putting the next line in the stuct i am trying to populate?

    Also, how do I parse out the Type from the # in the second part?

    I will be needing an array of structs for each part, I assume, but I really just need to know the best way of taking in the data and storing it in my struct.

    Thanks in advance,

    Evan

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fgets stops a a newline. That's how you get it to stop. Combine fgets with sscanf or manually checking the Nth position in the string, to decypher what each line contains.

    I'll leave that as an exercise to the OP.

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

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok I understand what you mean to an extent but what I fail to understand is how you are laying out your data in the file. Using a dynamically expanding array or linked lists can make a short work of the unknown size issue. The part where I am confused will require more detail before I can give you a specific example.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I suspect that the data file will look like so:
    Code:
    [
    ...some identifier...
    ...who the hell knows...
    ...something else...
    
    
    
    ]
    (
    int 30
    float 40.5
    arbitrary 12313
    fooinary -34
    barlean 0
    bazerific 3.3.4
    )
    [
    ...some identifier...
    ...who the hell knows...
    ...something else...
    
    
    
    ]
    (
    int 30
    float 40.5
    arbitrary 12313
    fooinary -34
    barlean 0
    bazerific 3.3.4
    )
    First, you skip everything until you hit the opening bracket: [
    Then you read one line into X
    The second line goes into Y
    The third line goes into Z
    Then you skip everything until you run into: ]
    Then you skip everything until you run into: (
    Now you read each line, putting the first portion of it into A, and the second into B.
    You repeat this until you run into the terminator, ).
    Then you start all over again.

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

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    11
    The files is organized as quzah said, but the ( blah blah ) sections always have - between the type and the number, like this

    (
    int-30
    float-402
    arbitrary-12313
    fooinary-34
    barlean-0
    bazerific-213
    )


    So the usage of fgets is
    char *fgets(char *buffer, int size, FILE *stream);

    What does the buffer do?
    the size limits the length of the read, the file is the source, but in an example like:

    cptr = fgets(buffer, 256, stdin);

    if you are assigning it to a ptr, then what does the buffer do?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You usually only use the return value to see if the function call was successful. You pass it a buffer you want filled, as illustrated in this FAQ entry under Option 3.

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

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    11
    Hmm. I can't really make much sense of that FAQ. It seemed like atoi and strtol work when there is a number first, not characters first.

    also, when you say:
    First, you skip everything until you hit the opening bracket: [
    Then you read one line into X
    The second line goes into Y
    The third line goes into Z
    Then you skip everything until you run into: ]
    Then you skip everything until you run into: (
    Now you read each line, putting the first portion of it into A, and the second into B.
    You repeat this until you run into the terminator, ).


    If i were to use a loop, what kind of loop would be best?
    How would you skip everything until you hit the desired part?


    By the way, this is EXTREMELY helpful, I am learning alot
    Thanks,
    Evan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing Info From file into Array of structs
    By bigparker in forum C Programming
    Replies: 16
    Last Post: 06-27-2009, 02:21 AM
  2. Help with file I/O
    By cantore in forum C Programming
    Replies: 5
    Last Post: 04-02-2006, 02:05 PM
  3. Replies: 5
    Last Post: 10-02-2005, 12:15 AM
  4. Reading from file into structs..
    By dankas in forum C Programming
    Replies: 14
    Last Post: 10-16-2002, 10:33 PM
  5. Thick n00b needs help with File i/o and structs
    By Catif in forum C++ Programming
    Replies: 3
    Last Post: 05-10-2002, 08:28 AM

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