Another interesting C question

EvanB

Senior member
Nov 3, 2001
268
0
0
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 have it skip the blank lines in the [ blah blah ] listing?

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.


So I basically need to

First, skip everything until you hit the opening bracket: [
Then 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, ).

So Im going to be using a loop I assume, but how do i do the skipping? I know Ill use fgets to get the strings, but beyond that im not sure

Thanks in advance,

Evan
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
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.
You don't really define what you mean by "Type-#" or "the types."

Perhaps you should list some sample data?
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Your logic is correct. Read in each line, analyze it and populate as needed.

For the type-# lines, you will need to either have a constant delimeter to seperate the two pieces of information or a list of valid characters sequences for the first part of the data.

If the - is a constant delimeter between the two data types, then parse accordingly.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
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 have it skip the blank lines in the [ blah blah ] listing?

Read the man page, it stops after the first newline character. The tricky part is knowing how big of a buffer you need. You either need to know the length of the longest possible line or you need to wrap the i/o in your own function that creates a dynamic sized buffer. To skip the blank lines just read them in and ignore them or, if you are very confident that they will be exactly that (3 blank lines) use fseek to jump over them (how far to jump depends on the system handles newlines so it's not portable this way).