C++ 读取文件内容到指定类型的变量

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/vernice/article/details/70150568
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

int main(){
	cout << "input the file name: ";
	string file_name;
	cin >> file_name;
	cout << endl;

	// ifstream infile("1.txt");
	ifstream infile(file_name.c_str());
	string line;
	while (std::getline(infile, line)){
		std::istringstream iss(line);
		int a, b;
		if (!(iss >> a >> b)) { break; } // error
		cout << "a : " << a << endl;
		cout << "b : " << b << endl;
	}
	return 1;
}

猜你喜欢

转载自blog.csdn.net/vernice/article/details/70150568