Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does fread fail for large files?

I have to analyze a 16 GB file. I am reading through the file sequentially using fread() and fseek(). Is it feasible? Will fread() work for such a large file?

like image 301
bcubed Avatar asked Sep 29 '10 21:09

bcubed


People also ask

Can fread fail?

If fread fails, it will typically keep failing. Typically because it hit the end of file, but possibly for some other reason. If it fails, you would normally not try again.

Does fread read whole files?

Return Valuefread returns the number of full items actually read, which may be less than count if an error occurs or if the end of the file is encountered before reaching count . Use the feof or ferror function to distinguish a read error from an end-of-file condition.

Why fread returns 0?

It returns 0 if no items are read because of an error or an immediate end of file. When using fread , remember that size is not necessarily a multiple of the record size, and that fread ignores record boundaries. The return value from fread does not indicate whether the call is completely successful.

What does fread() do?

The fread() function reads up to count items of size length from the input stream and stores them in the given buffer. The position in the file increases by the number of bytes read.


1 Answers

You don't mention a language, so I'm going to assume C.

I don't see any problems with fread, but fseek and ftell may have issues.

Those functions use long int as the data type to hold the file position, rather than something intelligent like fpos_t or even size_t. This means that they can fail to work on a file over 2 GB, and can certainly fail on a 16 GB file.

You need to see how big long int is on your platform. If it's 64 bits, you're fine. If it's 32, you are likely to have problems when using ftell to measure distance from the start of the file.

Consider using fgetpos and fsetpos instead.

like image 168
David Thornley Avatar answered Sep 19 '22 23:09

David Thornley