C++20标准引入了许多实用的新特性,其中std::format为字符串格式化提供了更优雅的方案,同时结合编译器和标准库的能力,我们可以更便捷地获取源文件位置,进而完成文件读取的相关操作。本文将围绕这两个核心点,详细讲解利用C++20标准读取文件内容的完整实现方式。

获取源文件位置的方法
在C++20中,我们可以通过__builtin_FILE()等编译器内置宏获取当前源文件的路径,不同编译器的内置宏略有差异,但核心逻辑一致。以GCC和Clang为例,我们可以通过以下方式获取源文件所在目录:
#include <string>
#include <filesystem>
// 获取源文件所在目录
std::filesystem::path get_source_dir() {
// __builtin_FILE() 返回当前源文件的完整路径,需要编译器支持
std::string source_path = __builtin_FILE();
std::filesystem::path path_obj(source_path);
// 返回源文件所在的目录路径
return path_obj.parent_path();
}
如果是MSVC编译器,可以使用__FILE__宏来获取源文件路径,用法和上述逻辑类似,只需要将__builtin_FILE()替换为__FILE__即可。
结合std::format处理文件路径
std::format是C++20引入的字符串格式化工具,相比传统的sprintf或者stringstream,它的语法更简洁,类型安全性更高。我们可以用它来拼接文件路径,比如拼接源文件目录和目标文件名:
#include <format>
#include <string>
#include <filesystem>
// 拼接源文件目录和目标文件名,得到完整文件路径
std::filesystem::path get_target_file_path(const std::string& file_name) {
std::filesystem::path source_dir = get_source_dir();
// 使用std::format拼接路径,注意路径分隔符的处理
std::string full_path_str = std::format("{}/{}", source_dir.string(), file_name);
return std::filesystem::path(full_path_str);
}
这里需要注意,不同操作系统的路径分隔符不同,Windows使用反斜杠,Linux和macOS使用正斜杠/,而std::filesystem::path会自动处理不同系统的路径分隔符,因此我们也可以直接使用路径拼接运算符:
std::filesystem::path get_target_file_path_v2(const std::string& file_name) {
std::filesystem::path source_dir = get_source_dir();
// 直接使用/运算符拼接路径,std::filesystem会自动适配系统
return source_dir / file_name;
}
读取文件内容的完整实现
结合前面的源文件位置获取和路径处理,我们可以使用C++20的std::ifstream配合std::format完成文件读取,同时可以对读取到的内容进行格式化处理:
#include <iostream>
#include <fstream>
#include <string>
#include <format>
#include <filesystem>
#include <vector>
// 读取文件内容并返回字符串向量,每行作为一个元素
std::vector<std::string> read_file_content(const std::filesystem::path& file_path) {
std::vector<std::string> content;
std::ifstream file(file_path);
if (!file.is_open()) {
// 使用std::format格式化错误信息
std::string error_msg = std::format("无法打开文件: {}", file_path.string());
std::cerr << error_msg << std::endl;
return content;
}
std::string line;
int line_num = 1;
while (std::getline(file, line)) {
// 为每一行内容添加行号前缀,使用std::format格式化
std::string formatted_line = std::format("第{}行: {}", line_num, line);
content.push_back(formatted_line);
line_num++;
}
file.close();
return content;
}
int main() {
// 要读取的文件名,假设和源文件在同一目录
std::string target_file = "test.txt";
std::filesystem::path file_path = get_target_file_path(target_file);
std::cout << std::format("要读取的文件路径: {}", file_path.string()) << std::endl;
std::vector<std::string> file_content = read_file_content(file_path);
for (const auto& line : file_content) {
std::cout << line << std::endl;
}
return 0;
}
注意事项
- 使用
__builtin_FILE()宏需要编译器支持,GCC 4.8+、Clang 3.2+都支持该内置宏,MSVC没有该内置宏,直接使用__FILE__即可。 - std::format需要编译器支持C++20标准,GCC 13+、Clang 14+、MSVC 19.29+都提供了对std::format的支持,编译时需要开启对应的C++20标准选项,比如GCC使用
-std=c++20。 - 如果读取的文件路径包含中文等宽字符,需要确保编译器和文件系统的编码一致,避免出现乱码问题。
总结
通过C++20的__builtin_FILE()或__FILE__宏我们可以获取源文件位置,结合std::filesystem可以构建准确的文件路径,再使用std::format完成路径拼接和内容格式化,最后通过std::ifstream完成文件内容的读取。这种方式既利用了C++20的新特性,也让文件读取的逻辑更清晰、代码更简洁。
C++20读取文件std_format源文件位置文件操作修改时间:2026-06-20 19:42:19