您的位置:首页 > 编程语言 > C语言/C++

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

2017-04-13 00:52 337 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: