【发布时间】:2012-10-09 07:00:45
【问题描述】:
可能重复:
Using C++ filestreams (fstream), how can you determine the size of a file?
file size in c program
我想检索文件的大小,为此我以这种方式实现
(void)fseek(m_fp, 0, SEEK_END); // Set the file pointer to the end of the file
pFileSize = ftell(m_fp); // Get the file size
(void)fseek(m_fp, oldFilePos, SEEK_SET); // Put back the file pointer at the original location
这是不可行的,有没有其他方法可以获取文件大小或检查文件是否包含数据
【问题讨论】:
-
您使用的是哪个操作系统?
-
为什么这样做不可行?
-
@moooeeeep 因为它调用未定义的行为?来自 C 标准:
A binary stream need not meaningfully support fseek calls with a whence value of SEEK_END.、Setting the file position indicator to end-of-file, as with fseek(file, 0, SEEK_END), has undefined behavior for a binary stream... -
如果你不怕前沿,
std::tr2::file_size