1. 给出两个文本文件:
file1:
中国 china
你好 hellofile2:
中国 china
你好 hello
2. 从文本file1中读取每行并输出;或者 从文本file2中读取每个单词并输出。
3.代码如下:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
//read contents from txt into a fstream object "inFile1".
fstream inFile1("file1.txt");
//情况1:读取文本中每一行内容,并输出.
string line;
//read every line in "inFile" and putout them.
while(getline(inFile1,line))
//process(line);
cout << line << endl;
//read contents from txt into a fstream object "inFile2".
fstream inFile2("file2.txt");
//情况2:读取文本中每一单词内容,并输出.
string word;
//read every word in "inFile" and putout them.
while(inFile2 >> word)
//process(word);
cout << word << ",";
return 0;
}

本文档介绍如何从文本文件中读取内容。提供了从file1逐行读取并输出,以及从file2逐词读取并输出的代码示例,详细展示了读取过程和运行结果。
最低0.47元/天 解锁文章
1544

被折叠的 条评论
为什么被折叠?



