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

时间:2022-05-15 04:59:01

如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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++ 读取文件内容到指定类型的变量方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/vernice/article/details/70150568