存储.dat文件中的数据

时间:2013-07-08 01:53:24

标签: c++

我正在尝试存储.dat文件中的数据。使用while循环和ifstream我已经设法让它打印出我需要的东西,但我需要存储它打印出来的东西,这样我就可以对它们进行算术运算。从打印信息到存储信息似乎是如此短暂的飞跃,但我无法弄明白。

这是我到目前为止的代码:

int main()
{
    char name;
    cin.get(name);

    ifstream inStream;
    inStream.open("grade.dat");

    while (name != ' ')
    {
        inStream.get(name);
        cout << name;    
    }

    return 0;
}

1 个答案:

答案 0 :(得分:3)

您只需将它们全部放在某种数据结构中即可。因为您正在使用C ++,所以我建议使用STL数据结构之一。幸运的是,有人已经询问如何从文件中读取文本并将其存储在STL vector中!

Reading line from text file and putting the strings into a vector?