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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With