C++ 读取文本, 读取( 单字符/ 一行/ 全部 )
2024-10-21 23:11:51
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;
}
}
最新文章
- WebForm 常用控件
- Linux下创建文本文件(vi/vim命令使用详解)
- CF 702B Powers of Two(暴力)
- jsf primefaces note
- [工具]Mac平台开发几个网络抓包工具(sniffer)
- Linux启动过程详解 (转)
- HDU 2227-Find the nondecreasing subsequences(dp+BIT优化)
- ASP.NET MVC轻教程 Step By Step 10——模型验证
- 一:详解 HTTP 协议
- ES6.3.2 index操作源码流程
- day 7-13 数据库的数据类型
- 网络:LVS负载均衡原理
- 【 Gym - 101124E 】Dance Party (数学)
- Codeforces Round #516 (Div. 2) (A~E)
- 8.ajax与django后台json数据的交互
- os模块学习+open行数
- Jmeter实现对字符串加密
- .NET 与MVC的区别
- 一点对Promise的理解与总结
- MySQL8 Authentication plugin &#39;caching_sha2_password&#39; cannot be loaded
热门文章
- awk 计算
- dropbear 2019.78 Installing to target......Running build_buildroot failed!
- 修改jar包内容
- 将【jar包、bat、其他文件】注册到windows服务的三种方法
- Spring基本概述
- Javascript中求Date类型的差值、增加/减少秒/分钟/小时/天等
- Pytorch实战学习(八):基础RNN
- 》》》Java利用aspose-words将word文档转换成pdf(破解 无水印)
- iptables( <; deb ufw)
- 专业视频图片水印清除工具丨HitPaw Watermark Remover 2.1.3