小编Usm*_*eer的帖子

从C++中的文本文件中读取数字数据

例如,如果外部文本文件中的数据是这样的:

45.78   67.90   87
34.89   346     0.98
Run Code Online (Sandbox Code Playgroud)

如何阅读此文本文件并将每个数字分配给c ++中的变量?使用ifstream,我可以打开文本文件并为变量分配第一个数字,但我不知道如何读取空格后的下一个数字.

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    float a;
    ifstream myfile;
    myfile.open("data.txt");
    myfile >> a;
    cout << a;
    myfile.close();
    system("pause");
    return 0;
}

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    int data[6], a, b, c, d, e, f;
    ifstream myfile;
    myfile.open("a.txt");

    for(int i = 0; i << 6; i++)
        myfile >> data[i];

    myfile.close();
    a = data[0];
    b = data[1];
    c = data[2];
    d = data[3]; …
Run Code Online (Sandbox Code Playgroud)

c++

39
推荐指数
3
解决办法
28万
查看次数

标签 统计

c++ ×1