-
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
-
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.
-
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.
-
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.
-
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?
-
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.
-
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