C++ 读取文件所有内容的方法
2023-09-14 09:07:08 时间
方法一
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
ifstream ifs("test.txt");
string content( (istreambuf_iterator<char>(ifs) ),
(istreambuf_iterator<char>() ) );
cout << content << endl;
ifs.close();
return 0;
}
方法二
#include <fstream>
#include <vector>
using namespace std;
int main(int argc, char** argv) {
ifstream ifs("test.txt");
// get the size of file
ifs.seekg(0, ios::end);
streampos length = ifs.tellg();
ifs.seekg(0, ios::beg);
vector<char> buffer(length);
if (ifs.read(buffer.data(), length)) {
// process
ofstream out("output.txt");
out.write(buffer.data(), length);
out.close();
}
ifs.close();
return 0;
}
方法三
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
int main(int argc, char** argv) {
std::ifstream t("file.txt");
std::stringstream buffer;
buffer << t.rdbuf();
std::string contents(buffer.str());
// process
t.close();
return 0;
}
相关文章
- C++输入输出文件流[通俗易懂]
- c++时间戳转换日期格式_java时间戳转换成时间
- C++ 分区、文件夹大小获取、文件数据操作demo示例
- EasyC++25,三元表达式
- EasyC++65,运算符重载限制
- C++读写文件操作(fstream、ifstream、ofstream、seekg、seekp、tellg、tellp用法)[通俗易懂]
- 判断DLL文件是C/C++动态链接库还是.NET程序集及查看DLL依赖
- C++提高编程笔记合集
- c++报错无法打开文件_如何打开源文件
- c++ 分苹果_接苹果问题C语言
- C++stl库_c++库
- C++基本概念_c语言 c++区别
- c++读取json文件_cfile读写文件
- 论c++中的文件操作(竞赛必看)通俗易懂
- C/C++ Qt 基本文件读写方法
- C/C++ MinHook 库的使用技巧
- C/C++ Qt 监控文件状态变化
- 【Android NDK 开发】NDK C/C++ 代码崩溃调试 - Tombstone 报错信息日志文件分析 ( 使用 addr2line 命令行工具查找动态库中的报错代码位置 )
- C++文件读写详解(ofstream,ifstream,fstream)编程语言
- C++命名空间在多文件编程中的具体用法
- C++标准之(ravaluereference)右值引用介绍
- C++文件依存关系介绍
- C字符串与C++字符串的深入理解
- C++中的函数指针与函数对象的总结
- android杂记:C++文件的添加log方法分享
- C++获取文件哈希值(hash)和获取torrent(bt种子)磁力链接哈希值
- c++mk文件出错Jni调用产生java.lang.UnsatisfiedLinkError错误解决方法
- C++判断pe文件实例
- C++遗传算法类文件实例分析