Solved

Urgent:fread

Posted on 2008-06-20
8
249 Views
Last Modified: 2010-04-21
hi,

i am doing this with fread and i get ??? marks..could anyone pls help...thanks

char *t;
t=(char *)malloc(28);

      fread(t,sizeof(char),28,fp);
t[9]='\0';
      
      printf("%s\n",t);

0
Comment
Question by:zizi21
  • 5
  • 3
8 Comments
 
LVL 11

Expert Comment

by:spoxox
ID: 21835937
What file are you trying to read? Your code will read from a file. Make sure you have successfully opened a file for reading.
char t[10];
 

FILE *fp;

fp = fopen("dat\\test.dat", "r");

if (fp==NULL) {

  printf("Error: can't open file.");

  return -1;

}
 

fread(t,sizeof(t),8,fp);

t[9]='\0';

printf("%s\n",t);

Open in new window

0
 
LVL 11

Expert Comment

by:spoxox
ID: 21835944
Hey, the question changed!
0
 

Author Comment

by:zizi21
ID: 21835956
hi..you are right....i was trying few ways....i was able to edit it..dont worry...i would accept the solution if it works....
0
 

Author Comment

by:zizi21
ID: 21835968
hi, it doesn't work..
the reason i edited the question is because i found any link here which teaches you with malloc..with the current way, you get segementation faults, bus errors...i would go with the edited question then...thanks..
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Accepted Solution

by:
spoxox earned 500 total points
ID: 21835974
Still works for me. A couple of other points.

1) Note the alternate use of malloc on line 1.

2) Definitely want to make sure file is opened successfully: lines 3...8

3) Browse the file to make sure it's not empty.

4) Note the change in fread in line 10. The second parameter is the size of each element to be read. It should not be sizeof(t), as t is a pointer - unless you are reading pointers. It looks like you want to read characters.
char *t = malloc(28 * sizeof(char));
 

FILE *fp;

fp = fopen("dat\\test.dat", "r");

if (fp==NULL) {

  printf("Error: can't open file.");

  return -1;

}
 

fread(t, sizeof(char), 8, fp);

t[9]='\0';

printf("%s\n",t);

Open in new window

0
 

Author Comment

by:zizi21
ID: 21835984
hi there,

you are right...when i was doing make clean..not targets was deleted...thats why..it works..thank you...


0
 

Author Closing Comment

by:zizi21
ID: 31469365
excellent!
0
 

Author Comment

by:zizi21
ID: 21835989
hi ,

what do you mean by browse the file...how do you do that?
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now