<filesystem>
(在C ++17中添加)使得这个非常简单明了。
#include <cstdint>
#include <filesystem>
// ...
std::uintmax_t size = std::filesystem::file_size("c:\\foo\\bar.txt");
如评论中所述,如果您计划使用此函数来决定从文件中读取多少字节,请记住,在您请求文件大小和尝试从文件中读取数据的时间之间,除非该文件是由您独占打开的,否则其大小可能会发生更改。std::uintmax_t
能够保存比 std::size_t
更大的值?如果没有,为什么不使用更容易识别的 std::size_t
?顺便赞一个答案。 - Fureeishfile_size
返回的类型,这只是因为它的返回类型如此。对我来说也有点奇怪。 - HolyBlackCatstd::size_t
只需要能够容纳内存中对象的最大尺寸。文件可以比内存对象大得多。 - Richard Crittensize_t
是32位,而uintmax_t
是64位。 - HolyBlackCatC++17带来了std::filesystem
,它简化了许多与文件和目录相关的任务。你不仅可以快速获取文件大小、属性,还可以创建新目录、迭代文件、使用路径对象。
这个新库给我们提供了两个可用的函数:
std::uintmax_t std::filesystem::file_size( const std::filesystem::path& p );
std::uintmax_t std::filesystem::directory_entry::file_size() const;
第一个函数是std::filesystem
中的一个自由函数,第二个函数是directory_entry
中的一个方法。
每个方法都有多种重载形式,因为它可能会抛出异常或通过输出参数返回错误代码。下面是详细的代码,解释了所有可能的情况。
#include <chrono>
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main(int argc, char* argv[])
{
try
{
const auto fsize = fs::file_size("a.out");
std::cout << fsize << '\n';
}
catch (const fs::filesystem_error& err)
{
std::cerr << "filesystem error! " << err.what() << '\n';
if (!err.path1().empty())
std::cerr << "path1: " << err.path1().string() << '\n';
if (!err.path2().empty())
std::cerr << "path2: " << err.path2().string() << '\n';
}
catch (const std::exception& ex)
{
std::cerr << "general exception: " << ex.what() << '\n';
}
// using error_code
std::error_code ec{};
auto size = std::filesystem::file_size("a.out", ec);
if (ec == std::error_code{})
std::cout << "size: " << size << '\n';
else
std::cout << "error when accessing test file, size is: "
<< size << " message: " << ec.message() << '\n';
}
<filesystem>
,该头文件提供了一组类和函数,用于管理文件系统中的文件和目录。这些类和函数包括std::filesystem::path
,它表示文件或目录的路径,并提供了许多有用的操作,例如连接、规范化和比较路径。std::filesystem::directory_entry
表示文件系统中的一个实体,可以查询其属性(如大小和修改时间)和操作(如重命名和删除)。此外,还提供了许多其他类和函数,例如std::filesystem::file_size()
和std::filesystem::create_directory()
。在使用<filesystem>
之前,您需要确保编译器支持C++17,并在编译时链接正确的库(例如,在Windows上,您需要链接-lstdc++fs
)。注意:<filesystem>
是C++17的一部分,但在某些平台上可能不受支持。如果您需要在旧版本的C++或不支持<filesystem>
的平台上编写代码,则可以使用第三方库,例如Boost.Filesystem。 - Richard Crittentellg
的问题。唯一值得关注的是第四个问题,但那个问题不是很好,因为它在问题和回答中都过多地提到了ofstream
。这个问题比其他问题更好地表达了意图(除了第一个有点奇怪被关闭的问题)。 - Nicol Bolasstat(2)
到底有什么问题?它是不是已经过时了? - Lorinczy Zsigmondstat(2)
有什么问题 它不是语言标准的一部分。 - Andrew Henle