Solved

Urgent:fread

Posted on 2008-06-20
8
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
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.

752 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