方法一

#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++ 读取文件所有内容的方法的更多相关文章

  1. Shell逐行读取文件的3种方法

    方法1:while循环中执行效率最高,最常用的方法. while read linedoecho $linedone  < filename 注释:这种方式在结束的时候需要执行文件,就好像是执行 ...

  2. PHP中读取文件的几个方法

    整理了一下PHP中读取文件的几个方法,方便以后查阅. 1.fread string fread ( int $handle , int $length ) fread() 从 handle 指向的文件 ...

  3. Java 读取文件的内容

    Java 读取文件的内容 1) CLASS_NAME: 换成自己真实的类名 2) /page/test.json: 换成自己真实的page 3) FileUtils: 来自于org.apache.co ...

  4. php 读取文件的几种方法

    文件操作的三个步骤,打开,操作,关闭.$fopen=fopen(路径,方式),fwrite($fopen,写入的字符串);fclose($fopen). 其中打开方式有如下几种方式: 模式 描述 r ...

  5. Java一次性读取文件的内容

    我们做文本处理的时候的最常用的就是读写文件了,尤其是读取文件,不论是什么文件,我都倾向于一次性将文本的原始内容直接读取到内存中再做处理,当然,这需要你有一台大内存的机器,内存不够者……可以一次读取少部 ...

  6. Java io实现读取文件特殊内容进行替换

    最近公司在做一个项目其中一个需求是读取文件中的特殊字符在其后进行添加或删除字符操作,本来想直接使用randomAccessFile按行读取,读取到特殊字符就进行添加或删除操作,但是randomAcce ...

  7. python_基础学习_01_按行读取文件的最优方法

    python 按行读取文件 ,网上搜集有N种方法,效率有区别,先mark最优答案,下次补充测试数据 with open('filename') as file: for line in file: d ...

  8. Shell按行读取文件的3种方法

    Shell按行读取文件的方法有很多,常见的三种方法如下: 要读取的文件: [root@mini05 -]# cat file.info 写法一: [root@mini05 -]# cat read1. ...

  9. shell脚本,按行读取文件的几种方法。

    第一种方法用while实现按读取文件.[root@localhost wyb]# cat a.txt 第一行 aaaaaa 第二行 bbbbbb 第三行 cccccc 第四行 dddddd 第五行 e ...

随机推荐

  1. CSS冲突1 要关掉的css在项目内:【material-table】 中 checkbox 点击checkbox无法选中or取消,点击旁边才能选中or取消

    CSS优先级: !important > 行内样式 > 内嵌样式|链接外部样式(哪个在后面哪个优先级大) id选择器 > class选择器 > 元素选择器 react中好像还不 ...

  2. ovn-kubernetes安装指南

    Master节点的安装 1.首先在master节点安装ovs和ovn: #!/bin/bash sudo apt-get install openvswitch-common openvswitch- ...

  3. 在setting中实现可拔插的插件功能实现

    1.setting配置信息 在配置文件中通过对插件进行注释来实现可拔插,例如在配置中将其注释,则在使用此功能不起作用,注释取消后则可正常使用 # 可拔插的插件(plugins) PLUGINS_DIC ...

  4. jupter nootbok 快捷键、NumPy模块、Pandas模块初识

    jupter nootbok 快捷键 插入cell:a b 删除cell:x cell模式的切换:m:Markdown模式 y:code模式 运行cell:shift+enter tab:补全 shi ...

  5. C#的Installer生成的msi的安装文件,安装新版本时提示:已经安装了该产品的另一个版本。无法继续安装此版本

    之前折腾了个C#的项目: WLW (Windows Live Writer) Plugin–InsertSkydriveFiles 然后又弄了个对应的Installer: [已解决]给一个C#的Dll ...

  6. 关于sails 初学者常见问题汇总

    http://sailsdoc.swift.ren/ 这里有 sails中文文档 一.安装时: 先装nodejs,成功标志 node -v 安装sails 全局安装 node install sail ...

  7. npm命令,查看当前npm版本,更新nmp到最新版本,安装sails

    打开Node.js command prompt 1 查看npm当前版本 npm -v 2 更新npm至最新版本 npm install npm@latest -g 3 安装sails  npm in ...

  8. PAT 1110 Complete Binary Tree[比较]

    1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary t ...

  9. ZenCart分类数量打折Category Quantity Discount插件

    附件:http://files.cnblogs.com/lzj87980239/Category_Quantity_Discount.rar 效果图后台1.将update.sql导入到数据库 2.将Y ...

  10. spark[源码]-SparkEnv执行环境创建

    sparkEnv概述 sparkEnv是spark的执行环境,其中包括众多与Executor执行相关的对象.在local模式下Driver会创建Executor,local-cluster部署模式或者 ...