2

可能重复:
使用 C++ 文件流 (fstream),如何确定文件的大小?
c程序中的文件大小

我想检索文件的大小,为此我以这种方式实现

(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

这对于出于需要是不可行的,还有其他方法可以获取文件大小或检查文件是否包含数据

4

3 回答 3

6

你可以使用统计

#include <sys/types.h>
#include <sys/stat.h>
#if defined (_WIN32)
#  include <io.h> // make portable for windows
#else
#include <unistd.h>
#endif

struct stat st;
stat (filename, &st);
std::cout << st.st_size;
于 2012-10-09T07:03:10.763 回答
0

如果是二进制流,那么 pFileSize 就是从 oldFilePos 到文件末尾的字节数。

于 2012-10-09T07:04:37.640 回答
0

你写的是一种 100% 多平台的方式来做你需要的事情。stat()在类 Unix 操作系统或GetFileAttributesEx()Windows上,使用您的操作系统调用来做更多事情。

于 2012-10-09T07:59:18.367 回答