迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

在 C++ 中获取文件大小

作者:迹忆客 最近更新:2024/01/02 浏览次数:

本文将演示如何在 C++ 中获取文件大小的多种方法。


使用 std::filesystem::file_size 函数在 C++ 中获取文件大小

std::filesystem::file_size 是 C++ 文件系统库函数,用于检索文件的字节大小。std::filesystem::file_size 使用文件的路径作为函数参数,其类型为 std::filesystem::path。在下面的例子中,我们传递了文件路径的字符串值,然后用它来构造相应的对象并检索文件大小。

#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::ifstream;

int main() {
  string path = "input.txt";

  cout << "size of file '" << path << "' = " << std::filesystem::file_size(path)
       << endl;

  return EXIT_SUCCESS;
}

输出:

size of file 'inbox.txt' = 10440

使用 std::filesystem::file_sizestd::filesystem::path 在 C++ 中获取文件大小

std::filesystem::file_size 函数的另一种用法是将文件路径值作为 path 类型变量插入。首先,你需要在使用类型之前包含 using std::filesystem::path 语句。接下来,我们可以声明 path 对象,如下面的示例代码所示,并用一个字符串文字来初始化它。构造的变量被传递给 file_size 函数以获取文件大小。

#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::error_code;
using std::ifstream;
using std::string;
using std::filesystem::path;

int main() {
  class path fd_path = "input.txt";

  cout << "size of file '" << fd_path
       << "' = " << std::filesystem::file_size(fd_path) << endl;

  return EXIT_SUCCESS;
}

输出:

size of file 'inbox.txt' = 10440

使用 std::filesystem::file_sizeerror_code 在 C++ 中获取文件大小

由于 std::filesystem::file_size 利用底层操作系统服务来检索文件的大小,所以很有可能会失败,引发一些异常。为了更好地处理每次 file_size 函数调用,建议使用 error_code 对象作为第二个参数,并存储失败时的错误信息。因此,我们可以评估 error_code 对象,并使用 if...else 语句实现错误处理代码。

#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::error_code;
using std::ifstream;
using std::string;
using std::filesystem::path;

int main() {
  string path = "input.txt";
  error_code ec{};

  auto size = std::filesystem::file_size(path, ec);
  if (ec == error_code{})
    cout << "size of file '" << path << "' = " << size << endl;
  else
    cout << "Error accessing file '" << path << "' message: " << ec.message()
         << endl;

  return EXIT_SUCCESS;
}

输出:

size of file 'inbox.txt' = 10440

使用 stat 函数在 C++ 中获取文件大小

stat 是符合 POSIX 标准的函数,在多个操作系统中都可以使用。它通常是 std::filesystem::file_size 方法下面的函数。stat 以字符串作为第一个参数,表示 struct stat 类型对象的文件地址的路径作为第二个参数。

struct stat 是一个特殊的预定义对象,在 stat 函数调用成功后,存储多个文件参数。需要注意的是,我们只需要访问 struct stat 对象中的 st_size 数据成员,它以字节数的形式存储文件大小。

#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::string;

int main() {
  string path = "input.txt";
  struct stat sb {};

  if (!stat(path.c_str(), &sb)) {
    cout << "size of file '" << path << "' = " << sb.st_size << endl;
  } else {
    perror("stat");
  }

  return EXIT_SUCCESS;
}

输出:

size of file 'inbox.txt' = 10440

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便