Reading Lines from File in C++

Reading Lines from File in C++

In C++, istringstream has been used to read lines from a file.

code:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

int main() {
	ifstream fin("vocab.gbk.txt");
	string line, word;
	istringstream iss;
	while (getline(fin, line)){
		iss.clear();
		iss.str(line);
		while (iss >> word) {
			cout << word << " ";
		}
		cout << endl;
	}
	return 0;
}

file:

</s> 0
, 15533134
的 9841198
。 9088518
年 2951309
在 2790829
是 2185996

Output:

</s> 0
, 15533134
的 9841198
。 9088518
年 2951309
在 2790829
是 2185996

Ref:

<<C++ Primer 5th 中文版>> P288

原文地址:https://www.cnblogs.com/fengyubo/p/10254243.html