C++依次读取文件中的字符串——getline()函数的应用
来源:互联网 发布:淘宝床品拍摄灯光布置 编辑:程序博客网 时间:2024/06/06 01:50
例如文件test.txt中有这么一段话:I am a boy. You are a girl.
如何一次一个的读取单词,即第一次读取I,第二次读取am,依次类推。
方法1:
#include <fstream>#include <string>#include <iostream>using namespace std;int main(){ ifstream ifs("test.txt"); // 改成你要打开的文件 streambuf* old_buffer = cin.rdbuf(ifs.rdbuf()); string read; while(cin >> read) // 逐词读取方法一 cout << read; cin.rdbuf(old_buffer); // 修复buffer}
方法2:
#include <iostream>#include <fstream>using namespace std;int main(){ ifstream ifs("test.txt"); // 改成你要打开的文件 ifs.unsetf(ios_base::skipws); char c; while(ifs.get(c)) // 逐词读取方法二 { if(c == ' ') continue; else cout.put(c); }}
方法3:
#include <iostream>#include <fstream>#include <string>using namespace std;int main(){ ifstream ifs("test.txt"); // 改成你要打开的文件 string read; while(getline(ifs, read, ' ')) // 逐词读取方法三 { cout << read << endl; }}
方法4:
#include <fstream>#include <iostream>using namespace std;int main(){ ifstream ifs("test.txt"); // 改成你要打开的文件 char buffer[256]; while(ifs.getline(buffer, 256, ' ')) // 逐词读取方法四 { cout << buffer; }}
- C++依次读取文件中的字符串——getline()函数的应用
- C++依次读取文件中的字符串——getline()函数的应用
- C++——getline函数的使用
- 文件读取之getline函数
- getline()函数文件读取
- 依次读取文件中的一个字符
- getline()函数在文件读写中的应用详解
- 在c语言环境下实现 C++中的getline函数 即在c语言中按行读取
- C 中的getline函数需要=NULL.
- C/C++中的getline函数总结
- C/C++中的getline函数总结
- C/C++中的getline函数总结
- 关于c++STL中的getline()函数
- c++中的cin,cin.get(),cin.getline()读取字符串
- 用c#实现 统计一个文件夹中的文件的个数 并用 FOR循环 依次读取文件的文件名
- [C/C++标准库]_[初级]_[C++ iostream read getline 读取文件慢的原因]
- string类中getline函数的应用
- 使用ifstream和getline读取文件内容[c++]
- VC 切分窗口
- Linux 内核文件系统与设备操作流程分析
- 波音787 Dreamliner娱乐系统将选择Android
- Linux 信号signal处理机制
- WebService异步调用
- C++依次读取文件中的字符串——getline()函数的应用
- 如何让局域网其他用户访问虚拟机
- 《顺序结构的程序设计》教学设计
- t4 template & MSBuild 入门
- KMP模式匹配算法学习
- ubuntu中重启X
- 给表字段加个索引节省了我8S
- 软件工程三:需求分析20条法则(摘自软件工程专家网)
- 【9-16】C笔记