Sponsored Content
Top Forums Programming Malloc problem with fread() to read file to structure in C Post 302887790 by yifangt on Monday 10th of February 2014 09:39:52 PM
Old 02-10-2014
Just in case I forgot some lines that may have been modified, here I copy and paste the same code of my post #8 and compile in another PC with same Ubuntu:
Code:
yifangt@box $ gcc -o readf -Wall readfile.c  
yifangt@box $ ./readf 

24 bytes allocated at 0x1797250 for linked list element
7 bytes allocated at 0x1797270 for name Angola

24 bytes allocated at 0x1797290 for linked list element
10 bytes allocated at 0x17972b0 for name Bangalore

24 bytes allocated at 0x17972d0 for linked list element
6 bytes allocated at 0x17972f0 for name Cairo

24 bytes allocated at 0x1797310 for linked list element
7 bytes allocated at 0x1797330 for name Dallas

24 bytes allocated at 0x1797350 for linked list element
9 bytes allocated at 0x1797370 for name Edmonton

24 bytes allocated at 0x1797390 for linked list element
6 bytes allocated at 0x17973b0 for name Fargo

24 bytes allocated at 0x17973d0 for linked list element
8 bytes allocated at 0x17973f0 for name Georgia

24 bytes allocated at 0x1797410 for linked list element
8 bytes allocated at 0x1797430 for name Halifax

24 bytes allocated at 0x1797450 for linked list element
13 bytes allocated at 0x1797470 for name Indianapolis

24 bytes allocated at 0x1797490 for linked list element
8 bytes allocated at 0x17974b0 for name Jamaica

End-of-file found on input.

Printing list element located at 0x1797250:
roll_num: 10001    name: Angola
roll_num: 24736352    name: Bangalore
roll_num: 24736416    name: Cairo
roll_num: 24736480    name: Dallas
roll_num: 24736544    name: Edmonton
roll_num: 24736608    name: Fargo
roll_num: 24736672    name: Georgia
roll_num: 24736736    name: Halifax
roll_num: 24736800    name: Indianapolis
roll_num: 24736864    name: Jamaica

This is almost the same of your output of post #9, that roll_num are wrong except the first one, very similar to post #6 where the first half was not shown. I will try more test. Anyway, gdb is the another one that I am trying to get familiar with at the meantime.
And thank you pointing out that I'm not doing anything with atoi() , calloc() , exit() , fclose() , ferror() , fgets() , fopen() , fprintf() , free() , malloc() , printf() , strlen() , or strncpy() that should behave differently on OS X than it should on any Linux distribution. which I have no experience how they may cause difference.
 

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
set_color(1)                                                           fish                                                           set_color(1)

NAME
set_color - set_color - set the terminal color set_color - set the terminal color Synopsis set_color [-v --version] [-h --help] [-b --background COLOR] [COLOR] Description Change the foreground and/or background color of the terminal. COLOR is one of black, red, green, brown, yellow, blue, magenta, purple, cyan, white and normal. o -b, --background Set the background color o -c, --print-colors Prints a list of all valid color names o -h, --help Display help message and exit o -o, --bold Set bold or extra bright mode o -u, --underline Set underlined mode o -v, --version Display version and exit Calling set_color normal will set the terminal color to whatever is the default color of the terminal. Some terminals use the --bold escape sequence to switch to a brighter color set. On such terminals, set_color white will result in a grey font color, while set_color --bold white will result in a white font color. Not all terminal emulators support all these features. This is not a bug in set_color but a missing feature in the terminal emulator. set_color uses the terminfo database to look up how to change terminal colors on whatever terminal is in use. Some systems have old and incomplete terminfo databases, and may lack color information for terminals that support it. Download and install the latest version of ncurses and recompile fish against it in order to fix this issue. Version 1.23.1 Sun Jan 8 2012 set_color(1)
All times are GMT -4. The time now is 09:50 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy