iansjack wrote:
alexfru wrote:
fseek() to positions 0, 1, 2, 4, 8, etc, fgetc(), check for errors. You get the top bit of the size. Similarly you get the rest.
What happens if you have write-only access to the file?
If you can't read the file, what can you use its size for? OTOH, if you're only writing to a file, either you don't care about its size (e.g. it's a log that only grows) or you know exactly how much you're supposed to write since standard C has no functions to truncate files at specified size. I think other cases (e.g. calculating size of files in a directory, taking proper care of links and other peculiarities) are rare and/or need special handling anyway (e.g. POSIX functions, path manipulation / hierarchy navigation, etc). But basic file sizing for basic purposes (=find out its size in order to read or copy it) can be implemented nearly portably.
iansjack wrote:And how many system calls is that going to take on a multi-gigabyte file? All in the name of some illusory "portability" when any operating system will provide a single API call to determine the size of a file.
For a 4G-1B file you'd need something like:
31 fseek() calls for the most significant bit of size
1 fseek() call for bit 30
1 fseek() call for bit 29
...
1 fseek() call for bit 1
1 fseek() call for the least significant bit
Double that because of fgetc().
Less than 100 calls. Not too bad, IMO.