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:
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)
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)
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 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
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,
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
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)
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)
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)