Solved

Urgent:fread

Posted on 2008-06-20
8
275 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
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

792 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