You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// NOTE: This isn't portable due to lack of meaningful support of `SEEK_END`
fseek(file_handle, 0, SEEK_END);
// get size
size_tfile_size= (size_t)ftell(file_handle);
// seek to start again
fseek(file_handle, 0, SEEK_SET);
This is a generic function, provided by the library to ease reading of files into buffer objects.
Unfortunately, the current implementation makes use of fseek() using the SEEK_END argument, which according to the documentation:
Library implementations are allowed to not meaningfully support SEEK_END (therefore, code using it has no real standard portability).
This means that my current approach is non-portable, and although it appears to work on Linux, Mac and Windows, I don't want to take any chances!
Probably a solution which uses fstat() on Unix and the equivalent functions on Windows is the best approach, a lá this Stack Overflow answer: https://stackoverflow.com/a/238609/6177253
The text was updated successfully, but these errors were encountered:
Easier alternative: ready bytes from file into the buffer, resizing the latter until the file is empty, or we run out of memory.
Pros:
No need to write OS-specific code
No need to call fseek(), which might incur a performance penalty for large files. Feels a strange way to get a file's size anyway.
Cons:
May end up allocating a load of memory before failing if someone tries to make it read from a special block file like /dev/zero or similar ones.
I'm going to have to write another function for 'resizing a buffer'. On the other hand, maybe this is a good thing to have anyway!
It's a bit annoying to have to keep on reallocating memory for something as it grows in size. How much memory do we increase it by each time? We'll have to shrink it back down to its actual size once we've read the whole file.
Current code:
libsxbp/sxbp/utils.c
Lines 96 to 107 in 0b30978
This is a generic function, provided by the library to ease reading of files into buffer objects.
Unfortunately, the current implementation makes use of
fseek()
using theSEEK_END
argument, which according to the documentation:This means that my current approach is non-portable, and although it appears to work on Linux, Mac and Windows, I don't want to take any chances!
Probably a solution which uses
fstat()
on Unix and the equivalent functions on Windows is the best approach, a lá this Stack Overflow answer: https://stackoverflow.com/a/238609/6177253The text was updated successfully, but these errors were encountered: