Malloc problem with fread() to read file to structure in C
Hello,
I am trying to read a text file into linked list, but always got the first and last records wrong.
1) The problem looks related to the initialization of the node temp with malloc(), but could not figure it out. No error/warning at compiling, though.
2) The output file is empty, indicating line 30 ~ 40 are not working at all, commented along with Line 15 for first debug try.
3) If Line 10 is replaced with Line 11, what is the correct way to do it? Line 23 was used for a try, but did not work with segment fault error.
Trying to understand more on malloc() and FILE stream. Thanks a lot!
Last edited by yifangt; 02-10-2014 at 02:29 AM..
Reason: typo + highlight lines
Hello,
I am trying to read a text file into linked list, but always got the first and last records wrong.
That isn't surprising since your input file is 142 bytes long and you're therefore calling fread() twice.
Quote:
Originally Posted by yifangt
1) The problem looks related to the initialization of the node temp with malloc(), but could not figure it out. No error/warning at compiling, though.
Let's come back to this later.
Quote:
Originally Posted by yifangt
2) The output file is empty, indicating line 30 ~ 40 are not working at all, commented along with Line 15 for first debug try.
Note that the start of the comment on line 30 (marked in orange text in your code below) is not closed until line 40 (also marked in orange below). So, lines 30 to 40 are just a single comment to the C compiler and no code is being executed in this comment.
Quote:
Originally Posted by yifangt
3) If Line 10 is replaced with Line 11, what is the correct way to do it? Line 23 was used for a try, but did not work with segment fault error.
Trying to understand more on malloc() and FILE stream. Thanks a lot!
So the real question is: What are you trying to do?
You are using fread() to fill in an integer, a 120 byte character array and a pointer with a character string read from the file. That clearly isn't what you want, but I'm not sure what you do want. 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, but since you free the space for the 1st element of the list before reading the second element (which you don't see because you commented out lines 30-40), you don't really have a linked list.
If you are trying to read each line from your input file, convert the numeric string at the start of the line into an integer and store it into the roll_num element of a struct student, allocate space for and copy the name on the rest of the input line into an area allocated to hold that string, and set the pointer at the end of the structure to the next student's entry, then your code isn't doing any of those things.
If you verify that what I listed above is what you're trying to do, I'll try to come up with an example that shows how to do that in the next couple of days.
These 2 Users Gave Thanks to Don Cragun For This Post:
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.
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.
Why...? Because 'char *string1' is a pointer: A four-byte(on my system, 8-byte on yours) integer that describes a location in memory. The four bytes 'stri' aren't likely to refer to any valid memory, and even if we win the lottery and find a valid location, it probably won't be pointing to a location containing "string1\n".
So we do this:
What does it print now?
Oh, it stuffed it all into 'string1' because it didn't stop when the string did.
C library calls will not give your structures the special treatment to put anything but the file's literal contents in that memory? How would it? It doesn't know what your structure is:
It only knows it was told, 'put this many bytes at this memory', and it did.
C does not know your data structures. Only your own functions do, and then, only if you tell them what it is, and it's still up to you to use them -- it does what you tell it, nothing more. If you want things read in any way other than a raw block, you have to write that.
"C library calls do not know your data structures" is a ready answer to a lot of questions. Like, "will free() deallocate pointers inside a structure pointer I give it, or just the structure itself?" It does not, indeed cannot -- it would need to know what's inside it, and it doesn't. All it has is the 4/8 bytes of the pointer you passed into it, and a list(well, heap) of what pointers it gave you before.
Last edited by Corona688; 02-10-2014 at 02:40 PM..
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.
No! It printed exactly what you stored in those locations. Using fread() to copy ASCII characters from a file into an object of type int in your structure doesn't automatically convert from a string to an integer.
You need to use something like atoi() to convert from a string into an integer. Perhaps the following will show what you were doing (using a union instead of your structure to show what happens when you interpret characters from a string as an integer. Store the following program in a file named string_vs_int.c:
and run the commands:
You will get something like:
Then note that the 1st five characters in your input file are "10001". Your use of fread() reads characters into the int at the start of your structure, just like the sample program above does using the union. Note that the number in red from your quote matches the numbers in red from my sample program!
Note that the above was run on a Intel x86 family CPU with int being an integer type occupying 4 bytes. With a different sized int, you will get different results. With a different CPU you might get different results. (The byte order of least significant byte to most significant byte in an int varies from CPU architecture to CPU architecture.)
Quote:
Originally Posted by yifangt
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.
Yes, it is clear that you still don't understand pointers. Hopefully the sample program at the end of this post will provide an understandable template. And, it appears that you thought fread() reads lines from a file. But, fread() doesn't care about line boundaries; it just copies the specified number of bytes from your input file into the supplied buffer.
Quote:
Originally Posted by yifangt
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.
If you save, build, and run the following C program:
and you run it in the directory that contains the input file you specified in the 1st post in this thread, you'll get output similar to the following:
Obviously, the pointers malloc() and calloc() return to you will vary from OS to OS (and are even highly likely to change even if you run the same program with the same data on the same machine twice).
Hopefully, there are enough comments in the code to demonstrate how to build a linked list, load data into the structures in the linked list, walk through the linked list to get data out of the list, and to deallocate the data after it is no longer needed.
Note that I use fgets() to read lines (rather than fread() to read buffers) from the input file. Note that since your input file has data that has to be converted from strings to integers and has variable length names, each line is read into an input buffer and then a linked list element structure is allocated for each line read and space to hold the name found on that input line is allocated for that list element's name field. Then data is copied into those allocated spaces. That allocated space CANNOT be freed until it is no longer going to be used.
Always determine how much space you need, allocate that much space (or more), and then copy your data into that space -- in that order. If you copy data into an uninitialized pointer or copy more data into an allocated space than was allocated, hard-to-track-down strange things may happen when copying the data, in the statement after you copied the data, or hundreds of statements after you copied the data. Check, double-check, and triple-check that you have allocated the space you need, have copied the correct data into your allocated space, and don't use any allocated space after it has been freed. Know when you have arrays of characters that don't (always) have a null byte terminator and either make sure you add a null terminator or never treat that array as a string. (Did you notice that u.c[] in the 1st program in this posting is printed using the format string %.4s instead of %s? That was done because that array will not have a null byte string terminator for any command line argument that contains for or more characters!)
Note that a couple of comments in this code start with XXX. They are reminders for code that should be added, but is not included in this sample. Filling in those missing data verification checks is left as an exercise for the reader.
This User Gave Thanks to Don Cragun For This Post:
Hi Corona & Don, Thanks you both so much!
This is very helpful as I could not find these explanations in any of the C books. All the books I have read so far did not combine these two parts (Corona's explanation and Don's Linked_list). I will try to digest more.
By the way, Don's code was compiled without any warning, but the roll_num value of the structure was wrong from strange 9065056 instead of 10002 with increment of 64 there after, except the first node 10001, which is correct. I realized why you gave an introduction program string_vs_int at the beginning. I'll try to catch why only 10001 was correctly printed. Thank you very much!
What OS are you using?
What is the size of the following types in your programming environment:
int
char *
struct student *
struct student
What were the sizes of the buffers allocated by the calls to malloc() and calloc() that you deleted from the output my code produced?
The output you showed in message #6 in this thread is missing newline and tab characters that my code writes and has changed a tab character to a constant string of four spaces? Did you modify my code? Did you modify the output it produced (other than removing the first two sections of the output)? If these alterations were done by the way you copy and paste code for display here, please redirect the output to a file (such as output.txt) and upload that text.
Please also upload the source you compiled so I can compare it with what I compiled.
I am using Solaris 10u11 on x86 machine, i am root, i want to use a gcc compiled code to use read10/write10 function of USCSI solaris library to access data from a normal USB mass storage device.
I am able to open a device prior to sending USCSI command via IOCTL.
IOCTL command works ok as... (0 Replies)
How can I use malloc with copying/creating files?
Is this the correct way?
I'm a bit confused...
int in_fd;
int *out_fd;
char buffer;
in_fd = open(av, O_RDONLY);
out_fd = malloc(strlen(av)+strlen(av)+2);
sprintf"(buffer,%s/%s", av,av); (5 Replies)
hi
One of our SCO 5.0.6 server is crashing every second or third time during rebooting with kernel panic.
According to this SCO manual:
http://wdb1.sco.com/kb/showta?taid=106181&qid=1689366546&sid=504668569&pgnum=1
I've saved dump image into a floppy and I've done the following steps:
#... (0 Replies)
Hi,
I have a following problem in C.
I have a function A in which I used to call another function (function B) and pass an array of values through array variable by using below:-
foo=functionB(array);
In functionB, i used to just return some "values" (e.g return num;) in order to pass... (1 Reply)
Hi
When im listing (ls -al ) its listing directories without / at the end of directories
dir1 dir2 dir3
and i need to list directories with
dir1/ dir2/ dir3/ and this should not be made by command ls -F
/ should be embedded at the last
since one of the scripts reads directories... (1 Reply)
Hi All,
These are the two ways i tried to read file but i getting work with second one not with the first.
char buf; // Defining space for buf
ctrlfnum = fopen(filename_arr.control_fname,"r");
1) n = fread(buf,sizeof(buf),1,ctrlfnum); ============== (not works)
2) n =... (4 Replies)
Hi folks,
Need your help.
I am writing a KSH script to read a few commands from a file & execute.
I am using the following code to read the file line by line & excute each command. When I am printing each line I see it is printing properly but while excuting, the particular "ps" command... (5 Replies)
Hi,
I want to read the task_struct structure in Linux in order to get the names & pids of all processes.
How can this be done??
Thanks in adv,
molu (4 Replies)
Hello,
I am having a problem setting a range of numbers for the "case" structure.
I can use with no problems, but
when I use it doesn't work???
Does the case struture allow numeric ranges?
eg:
echo -e "enter number between 0 and 60: \c"
read $answer
case $answer in
) echo... (2 Replies)