有没有情况下,fseek的/ FTELL可以给出错误的文件的大小? [英] Are there cases where fseek/ftell can give the wrong file size?
问题描述
在C或C ++,可用于返回文件大小如下:
In C or C++, the following can be used to return a file size:
const unsigned long long at_beg = (unsigned long long) ftell(filePtr);
fseek(filePtr, 0, SEEK_END);
const unsigned long long at_end = (unsigned long long) ftell(filePtr);
const unsigned long long length_in_bytes = at_end - at_beg;
fprintf(stdout, "file size: %llu\n", length_in_bytes);
是否有开发环境,编译器,或操作系统可以从这个code返回错误文件的大小,根据填充或其他信息是针对具体情况?有没有改变在C或C ++ 1999年左右规格,这将有导致此code不再在某些情况下工作吗?
Are there development environments, compilers, or OSes which can return the wrong file size from this code, based on padding or other information that is situation-specific? Were there changes in the C or C++ specification around 1999, which would have lead to this code no longer working in certain cases?
有关这个问题,请假设我用标志 -D_FILE_OFFSET_BITS = 64 -D_LARGEFILE64_SOURCE = 1
编译加入大文件支持。谢谢你。
For this question, please assume I am adding large file support by compiling with the flags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE=1
. Thanks.
推荐答案
这不会对unseekable文件的工作就像的/ proc内/ cpuinfo
或为/ dev /标准输入
或的/ dev / tty的
,或得到与管道文件的popen
It won't work on unseekable files like /proc/cpuinfo
or /dev/stdin
or /dev/tty
, or pipe files gotten with popen
如果该文件是由另一个进程在同一时间写它不会起作用。
And it won't work if that file is written by another process at the same time.
使用 Posix的统计功能可能是更高效,更可靠。当然,这个功能可能无法使用在非POSIX系统。
Using the Posix stat function is probably more efficient and more reliable. Of course, this function might not be available on non Posix systems.
这篇关于有没有情况下,fseek的/ FTELL可以给出错误的文件的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!