今天就跟大家聊聊有关使用C++怎么读取文件内容到指定类型,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
如下所示:
#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;
}
看完上述内容,你们对使用C++怎么读取文件内容到指定类型有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注天达云行业资讯频道,感谢大家的支持。