Solved

Urgent:fread

Posted on 2008-06-20
8
269 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

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…
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 opening and writing to files 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.

810 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