텍스트에서 한 줄을 읽고 단어를 읽고 출력합니다

1527 단어
1. 두 개의 텍스트 파일을 제공합니다.
file1:
   china
   hello
file2:
   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;
}

4. 실행 결과는 다음과 같습니다.
5.            ,        .
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
	
	//read contents from txt into a fstream object "inFile".
	fstream inFile("file.txt");

	ofstream outFile("result.txt");
	
	string word;
	//read every word in "inFile" and put out them.
	while(inFile >> word)
		//process(word);
		outFile << word << " ";//          .
	
	return 0;
}

좋은 웹페이지 즐겨찾기