如下所示:
#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++ 读取文件内容到指定类型的变量方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
-
VLOOKUP函数常用套路大全
2026-05-15栏目: 教程
-
微信群聊上线表格接龙功能 v7.0.8版本及以上用户可使用
2026-05-15栏目: 教程
-
Facebook跌出全球品牌10强榜单,隐私和安全问题拖累
2026-05-15栏目: 教程
-
被吐槽:Google:Pixel 4可以在闭着眼睛的情况下解锁!
2026-05-15栏目: 教程
-
网页优化中如何分析并追赶对手
2026-05-15栏目: 教程
