C++获取文件的大小和创建时间
时间: 2024-11-22 22:41:29 浏览: 39
在C++中,获取文件的大小通常涉及到`fstream`库,而获取文件的创建时间和最后修改时间则需要第三方库如`boost/filesystem`。以下是基本步骤:
1. **获取文件大小**:
```cpp
#include <fstream>
#include <iostream>
std::ifstream file("your_file.txt"); // 替换为你想要检查的文件名
if (file.is_open()) {
std::streampos fileSize = file.tellg();
file.seekg(0, std::ios::beg); // 将文件指针移回开始以便读取其他信息
std::cout << "File size: " << fileSize << " bytes" << std::endl;
file.close();
} else {
std::cerr << "Failed to open the file." << std::endl;
}
```
2. **获取文件创建和修改时间(需第三方库)**:
```cpp
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
fs::path filePath("your_file.txt");
if (fs::exists(filePath)) {
fs::time_typecreateTime = fs::last_write_time(filePath);
fs::time_typemodifyTime = fs::last_access_time(filePath);
std::cout << "Creation time: " << fs::local_time(createTime) << std::endl;
std::cout << "Last modification time: " << fs::local_time(modifyTime) << std::endl;
} else {
std::cerr << "File not found." << std::endl;
}
```
请注意,如果你没有安装`boost.filesystem`库,你需要先编译并链接这个库。
阅读全文
相关推荐


















