如何获取文件的大小
How to get the size of a file
可能的重复项:
使用 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
这对于需要是不可行的,并且是获取文件大小或检查文件是否包含和数据的任何其他方法
您可以使用 stat
#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;
如果它是一个二进制流,那么 pFileSize 是从 oldFilePos 到文件末尾的字节数。
你写的是一种100%多平台的方式来做你需要的事情。使用操作系统的调用来执行更多操作,stat()
类 Unix 操作系统或 Windows 上的GetFileAttributesEx()
。
相关文章:
- C++ - 忽略并从其他文件获取数据
- 为什么从文件获取图标时应用程序有时会崩溃?
- 如何在 c++ 窗口中将参数和返回的退出值传递到批处理文件/从批处理文件获取返回的退出值
- Qt .pro 文件获取 Qtkit 名称
- 提升属性树:如何使用xml文件获取子树的子级
- 从C 中的二进制文件获取整数值时
- 如何在Windows上绕过主机文件获取真实的IP地址
- 如何使用 CPP/C# 从 VHDX 文件获取文件(或文件系统)
- 从读取到对象指针数组的文件获取段错误
- 是否可以从IOS项目中的cpp文件获取文件路径
- 将数据从头文件获取到源文件中的非成员函数中
- 从两个不同的文件获取输入时,输入会发生变化
- 从文件 C++ 获取整数
- 通过在 C++ 中使用发布模式二进制文件获取崩溃原因
- C++ 将值从文件获取到数组中
- 使用HTTP上载文件.获取错误:-HttpSendReuest 12005
- 正在从外部文件获取输入
- 读/写同一个文件(获取gmon.out)
- 从二进制文件获取输入时发生访问冲突错误
- 从特定文件获取函数指针