C++ 读取文本

介绍三种读取方式:

  • 逐字符读取(注意不是字节)
  • 读取一行
  • 读取全部

示例代码:

#include <iostream>
#include <string>
#include <fstream>
using namespace std; int main() { ifstream iopen;
iopen.open("/home/lfh/QtProjects/test/main.cpp",ios::in);
int Type = 2; // 1:逐字符, 2:按行读取, 3:读取全部 if ( Type == 1 ) {
cout << "逐字符读取 " << endl;
char a;
while ( iopen.get(a) ) {
cout<<a;
}
}
else if ( Type == 2 ) {
cout << "逐行读取 " << endl;
string str;
while (iopen) {
std::getline(iopen, str);
cout<<str<<endl;
}
}
else if ( Type == 3 ) {
cout << "读取全部 " << endl;
string str;
std::getline(iopen, str, '\0');
cout<<str<<endl;
}
}

最新文章

  1. WebForm 常用控件
  2. Linux下创建文本文件(vi/vim命令使用详解)
  3. CF 702B Powers of Two(暴力)
  4. jsf primefaces note
  5. [工具]Mac平台开发几个网络抓包工具(sniffer)
  6. Linux启动过程详解 (转)
  7. HDU 2227-Find the nondecreasing subsequences(dp+BIT优化)
  8. ASP.NET MVC轻教程 Step By Step 10——模型验证
  9. 一:详解 HTTP 协议
  10. ES6.3.2 index操作源码流程
  11. day 7-13 数据库的数据类型
  12. 网络:LVS负载均衡原理
  13. 【 Gym - 101124E 】Dance Party (数学)
  14. Codeforces Round #516 (Div. 2) (A~E)
  15. 8.ajax与django后台json数据的交互
  16. os模块学习+open行数
  17. Jmeter实现对字符串加密
  18. .NET 与MVC的区别
  19. 一点对Promise的理解与总结
  20. MySQL8 Authentication plugin &#39;caching_sha2_password&#39; cannot be loaded

热门文章

  1. awk 计算
  2. dropbear 2019.78 Installing to target......Running build_buildroot failed!
  3. 修改jar包内容
  4. 将【jar包、bat、其他文件】注册到windows服务的三种方法
  5. Spring基本概述
  6. Javascript中求Date类型的差值、增加/减少秒/分钟/小时/天等
  7. Pytorch实战学习(八):基础RNN
  8. 》》》Java利用aspose-words将word文档转换成pdf(破解 无水印)
  9. iptables( &lt; deb ufw)
  10. 专业视频图片水印清除工具丨HitPaw Watermark Remover 2.1.3