Thanks Don!
Two things I was trying to catch:
1) Read the file into linked list, each line for a node which is a structure with two members:
roll_num (int) and
name (char array or char *); This is similar to my old post when I tried to parse file as 4-line record, i.e. every four line is a record.
2) Save the linked list to a new file.
For point 1), the problem with my code is the first and last record (i.e. nodes) of the linked list were always wrong---printed incorrectly.
Quote:
808464433 1 Angola //cf 10001 Angola
?x.?0010 Ja1 //cf 10010 Jamaica
For Point 2) my code was just not working at all, so that the corresponding part
Line 30 ~ Line 40 were commented out.
For point 1) an extra question in my mind is using
char pointer instead of
char array, how to accomplish the same job? as pointer is still a big challenge for me, especially when dynamic memory allocation is related.
I am trying to understand the
FILE stream and memory allocation with
malloc() for file manipulation. (The next step is to do some manipulation of each node, say change each
roll_num, modify the
name, or add another member for each node, etc. But I am not able to do it at this moment.)
Since the size of your current structure is about 132 bytes long (4 byte int, 120 byte char array, and 8 byte pointer), you are reading the 1st 132 bytes from your input file into the first element of your linked list,
Yes, that's what I was trying. I had thought of wrong mem allocation, so that I was thinking to use char pointer for
name instead of char array for dynamic memory allocation. I know I am too far from this point.
So, lines 30 to 40 are just a single comment to the C compiler and no code is being executed in this comment.
Yes, I did that on purpose because it did not work.
If you verify that what I listed above is what you're trying to do,
Yes, that's what I was trying to do!
I'll try to come up with an example that shows how to do that in the next couple of days.
Great! Thank you very much for your time.