Malloc problem with fread() to read file to structure in C


 
Thread Tools Search this Thread
Top Forums Programming Malloc problem with fread() to read file to structure in C
Prev   Next
# 1  
Old 02-10-2014
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!
Code:
#include <stdio.h>
#include <stdlib.h>

struct student
{
int roll_num;
char name[120];        //Line 10
//char *name;         //Line 11
struct student *next;
};

int main ()
{
FILE *fptr;
// FILE *ofptr;         //Line 15

struct student *temp;

fptr = fopen("INFILE.txt","r");
temp = malloc(sizeof(struct student));
// temp->name = malloc(sizeof(char)*120);            //Line 23
while(fread(temp, sizeof(struct student), 1, fptr))
    printf("%d\t%s\n",temp->roll_num, temp->name);
free(temp);
fclose(fptr);

/***************************************************** For writing          //Line 30

ofptr = fopen("OUTFILE.tab","w");
temp = malloc(sizeof(struct student));

while(temp != NULL)
{
    fwrite(temp, sizeof(struct student), 1, ofptr);
    temp = temp->next;
}
fclose(ofptr);
free(temp);
*****************************************************/                 //Line 40
 return 0;
}
/* INFILE.txt:
10001 Angola
10002 Bangalore
10003 Cairo
10004 Dallas
10005 Edmonton
10006 Fargo
10007 Georgia
10008 Halifax
10009 Indianapolis
10010 Jamaica

OUTFILE.tab:
808464433    1 Angola
10002 Bangalore
10003 Cairo
10004 Dallas
10005 Edmonton
10006 Fargo
10007 Georgia
10008 Halifax
10009 Indianapolis
?x.?0010 Ja1

*/


Last edited by yifangt; 02-10-2014 at 02:29 AM.. Reason: typo + highlight lines
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Structure of USCSICMD and CDB for USCSI Read/Write

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)
Discussion started by: danish2012
0 Replies

2. UNIX for Dummies Questions & Answers

Malloc and File Creation

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)
Discussion started by: l flipboi l
5 Replies

3. SCO

Read error on bootinfo structure at 0x800

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)
Discussion started by: ccc
0 Replies

4. Programming

Problem in static structure array in C

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)
Discussion started by: ahjiefreak
1 Replies

5. Shell Programming and Scripting

problem with listing of directory structure

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)
Discussion started by: vasanthan
1 Replies

6. Programming

problem in reading file using fread

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)
Discussion started by: arunkumar_mca
4 Replies

7. Shell Programming and Scripting

File read & execute problem

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)
Discussion started by: tipsy
5 Replies

8. Programming

How to read task_struct process structure of Linux

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)
Discussion started by: molu
4 Replies

9. UNIX for Dummies Questions & Answers

Problem w. case structure

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)
Discussion started by: Joe54321
2 Replies

10. Programming

a problem about malloc()

1 . Thanks everyone who read the post. 2 . the programe is that : #include <stdio.h> #include <string.h> void do_it(char *p) { p = (char *) malloc(100); (void )strcpy(p,"1234"); } int main(void) { char *p; do_it(p); (void )printf("p = %s \n",p); (1 Reply)
Discussion started by: chenhao_no1
1 Replies
Login or Register to Ask a Question