Sponsored Content
Top Forums Programming Malloc problem with fread() to read file to structure in C Post 302887762 by yifangt on Monday 10th of February 2014 04:57:15 PM
Old 02-10-2014
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!
Code:
 *** ***
Printing list element located at 0x8a5250:
roll_num: 10001    name: Angola
Freeing name (0x8a5270) and list element (0x8a5250) space.
roll_num: 9065056    name: Bangalore
Freeing name (0x8a52b0) and list element (0x8a5290) space.
roll_num: 9065120    name: Cairo
Freeing name (0x8a52f0) and list element (0x8a52d0) space.
roll_num: 9065184    name: Dallas
Freeing name (0x8a5330) and list element (0x8a5310) space.
roll_num: 9065248    name: Edmonton
Freeing name (0x8a5370) and list element (0x8a5350) space.
roll_num: 9065312    name: Fargo
Freeing name (0x8a53b0) and list element (0x8a5390) space.
roll_num: 9065376    name: Georgia
Freeing name (0x8a53f0) and list element (0x8a53d0) space.
roll_num: 9065440    name: Halifax
Freeing name (0x8a5430) and list element (0x8a5410) space.
roll_num: 9065504    name: Indianapolis
Freeing name (0x8a5470) and list element (0x8a5450) space.
roll_num: 9065568    name: Jamaica
Freeing name (0x8a54b0) and list element (0x8a5490) space.


Last edited by yifangt; 02-10-2014 at 06:47 PM..
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
fread(3s)																 fread(3s)

Name
       fread, fwrite - buffered binary input/output

Syntax
       #include <stdio.h>

       size_t fread(ptr, size, nitems, stream)
       void *ptr;
       size_t size, nitems;
       FILE *stream;

       size_t fwrite(ptr, size, nitems, stream)
       void *ptr;
       size_t size, nitems;
       FILE *stream;

Description
       The  function  reads  into a block beginning at ptr, nitems of data of the size size (usually sizeof *ptr) from the named input stream.	It
       returns the number of items actually read.

       If stream is stdin and the standard output is line buffered, then any partial output line will be flushed before any call to to satisfy the

       The function appends, at most, nitems of data of the size size (usually sizeof *ptr) beginning at ptr  to  the  named  output  stream.	It
       returns the number of items actually written.

Return Values
       The and functions return 0 upon end of file or error.

See Also
       read(2), write(2), fopen(3s), getc(3s), gets(3s), printf(3s), putc(3s), puts(3s), scanf(3s)

																	 fread(3s)
All times are GMT -4. The time now is 03:17 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy