Archived

This topic is now archived and is closed to further replies.

"fread" ?

This topic is 5828 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a problem: I am reading datas from file using "fread". But now in the middle of programming it does not work. "fread" returns zero. Has anyone skills with this? (I tryed feof => I am not at the end of file) So what to do? fread(data,2,1,myfile); // - Does Not Work Please.

Share this post


Link to post
Share on other sites
this would be because you are only reading in a small part of the file!

fread( void *buffer, size_t size, size_t count, FILE *stream );

you got your buffer (data), your size is 1 and your count is 2, reading from myfile. yeh?

well, you are reading in only the first 2 bytes of the file. size of the number of bytes to read, count is how many times.

so you want fread(data,1,sizeof(data),myfile); //this will work

however, if you want to read in a file to a buffer, but dont know the size of the file, you could use:

int nFileSize = filelength(fileno(myfile)); //needs io.h for that

fread(data,1,nFileSize,myfile);

that help?

edit; returns 0 on your problem, fread returns the number of items read. so none are being read from your file.

is your data an int or something, or string? if it was an int, then it probably wouldnt work because their 4bytes, and you was reading 2 (so sizeof(data) will fix it) but is it was a string (char array) then prehaps not)

Edited by - Bezzant on January 1, 2002 10:47:19 AM

Share this post


Link to post
Share on other sites