C++ 如何使用C++;17
我应该知道,特定操作系统是否存在陷阱 这个问题有许多重复(,)的地方,但它们在几十年前就得到了回答。今天,在这些问题中,很多问题的答案都是非常高的,这是错误的 来自.sx上其他(旧QA)的方法C++ 如何使用C++;17,c++,c++17,filesize,c++-standard-library,C++,C++17,Filesize,C++ Standard Library,我应该知道,特定操作系统是否存在陷阱 这个问题有许多重复(,)的地方,但它们在几十年前就得到了回答。今天,在这些问题中,很多问题的答案都是非常高的,这是错误的 来自.sx上其他(旧QA)的方法 (包装器),使用系统调用 ,根据定义返回一个位置,但不一定是字节。返回类型不是int (在C++17中添加)实现了这一点 #包括 #包括 // ... std::uintmax\u t size=std::filesystem::file\u size(“c:\\foo\\bar.txt”); 如注
- (包装器),使用系统调用
- ,根据定义返回一个位置,但不一定是字节。返回类型不是
int
(在C++17中添加)实现了这一点
#包括
#包括
// ...
std::uintmax\u t size=std::filesystem::file\u size(“c:\\foo\\bar.txt”);
如注释中所述,如果您计划使用此函数来决定从文件中读取多少字节,请记住 …除非您以独占方式打开该文件,否则可以在您请求该文件和尝试从中读取数据之间更改其大小。
-尼古拉·博拉斯
C++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
中的一个自由函数,第二个是目录项中的一个方法
每个方法都有一个重载,因为它可以抛出异常或返回错误代码(通过输出参数)。
下面是解释所有可能情况的详细代码
#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';
}
#包括
#包括
#包括
名称空间fs=std::filesystem;
int main(int argc,char*argv[])
{
尝试
{
const auto fsize=fs::文件大小(“a.out”);
std::cout Starter for 10:@L.F.:好吧,第一个问题已经作为第二个问题的副本结束了,这解释了为什么第一个问题中被接受的答案是错误的。第三个问题是关于类似的tellg
问题。唯一值得烦恼的是第四个问题,这个问题不太好,因为它谈论的问题和答案中的ofstream
。这一项比其他项更能表达意图(第一项除外,它奇怪地关闭了)。请停止在你的问题和问题标题中添加不相关的信息。年份不相关;技术相关。stat(2)有什么问题
不管怎样?它已经太老了还是怎么了?@Lorinczyzigmondstat(2)有什么问题
它不是语言标准的一部分。有点离题:是否有一个世界,std::uintmax\t
能够容纳比std::size\t\t\u\t更大的值?如果没有,为什么不使用std::size\t
,这可以说是更容易识别的?+1,btw@Fureeish我使用它只是因为它是文件大小的类型代码>返回。在我看来也有点奇怪。@Fureeishstd::size\u t
仅用于保存内存中对象的最大大小。在32位窗口上(我假设在大多数现代32位平台上),文件可以大得多,@Fureeish-Well)size\u t
是32位,而uintmax\u t
是64位。@HolyBlackCat:最好说一下文件系统是全局的,因此除非文件是由您独占打开的,否则它的大小可以在您请求它的时间和您尝试从中读取数据的时间之间更改。“这”到底是什么?你能解释一下所有这些代码的用途吗,特别是当被接受的答案使用更少的代码时?
#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';
}