Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

do fread and fwrite have failure? how to handle this?

Tags:

c

file

stream

when I read from a file using fread (C language), the return value of fread sometimes would be 0.

As manual suggested:

fread() and fwrite() return the number of items successfully read or written

do I have to write code like this?

int bytes_read;
while((bytes_read = fread(buffer, sizeof(int), 1, fp)) == 0) {

}

do we always have to check whether fread or fwrite succeeded?

like image 664
Mickey Shine Avatar asked Feb 22 '12 05:02

Mickey Shine


People also ask

How do you know if fread fails?

If fread returns fewer than the requested number of records, you've either hit EOF or a serious read error. You can distinguish between them by checking feof() and ferror() . Similarly, if fwrite returns fewer than the requested number of records, you've either run out of disk space or hit a serious write error.

How do I know if fwrite was successful?

The fwrite() function returns the number of members successfully written, which may be less than nitems if a write error is encountered. If size or nitems is 0, fwrite() returns 0 and the state of the stream remains unchanged.

What is fread and fwrite?

The function fread() reads nmemb items of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr. The function fwrite() writes nmemb items of data, each size bytes long, to the stream pointed to by stream, obtaining them from the location given by ptr.

What is fread return when it fails?

The fread() function returns the number of full items successfully read, which can be less than count if an error occurs, or if the end-of-file is met before reaching count. If size or count is 0, the fread() function returns zero, and the contents of the array and the state of the stream remain unchanged.


2 Answers

No, there's no sense in doing a retry-loop like this if fread or fwrite returns fewer than the expected number of records read or written. That is to say, stdio is not like the low-level read and write operations that can result in "short" reads or writes.

If fread returns fewer than the requested number of records, you've either hit EOF or a serious read error. You can distinguish between them by checking feof() and ferror().

Similarly, if fwrite returns fewer than the requested number of records, you've either run out of disk space or hit a serious write error.

In any case, due to buffering stdio makes it essentially impossible to know how much was successfully written, so if you encounter a write error, you usually need to consider the file lost and abort the whole operation.

like image 169
R.. GitHub STOP HELPING ICE Avatar answered Nov 10 '22 16:11

R.. GitHub STOP HELPING ICE


http://pubs.opengroup.org/onlinepubs/000095399/functions/fread.html

Upon successful completion, fread() shall return the number of elements successfully read which is less than nitems only if a read error or end-of-file is encountered. If size or nitems is 0, fread() shall return 0 and the contents of the array and the state of the stream remain unchanged. Otherwise, if a read error occurs, the error indicator for the stream shall be set, and errno shall be set to indicate the error.

http://pubs.opengroup.org/onlinepubs/007904875/functions/fwrite.html

The fwrite() function shall return the number of elements successfully written, which may be less than nitems if a write error is encountered. If size or nitems is 0, fwrite() shall return 0 and the state of the stream remains unchanged. Otherwise, if a write error occurs, the error indicator for the stream shall be set,and errno shall be set to indicate the error

The ferror() or feof() functions must be used to distinguish between an error condition and an end-of-file condition.

like image 3
Akhil Thayyil Avatar answered Nov 10 '22 15:11

Akhil Thayyil