"fread" ?

Started by
1 comment, last by AcoGL 22 years, 3 months ago
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.
Advertisement
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
Do you open your file in binary or text mode (the default) ? Text mode files can cause weird things like this...

This topic is closed to new replies.

Advertisement