Solved

Urgent:fread

Posted on 2008-06-20
8
260 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

895 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

12 Experts available now in Live!

Get 1:1 Help Now