如何将文件中的变量存储到C ++中的结构中

时间:2014-02-28 20:56:36

标签: c++ arrays string text struct

好的,所以我认为我得到了它,但任何输入,如果这不是正确的方式去做。然而,这就是我得到的,它现在将所有数据存储到结构的正确变量中。我想确保这个效果和我能得到的一样高效。感谢您提供任何帮助或意见。

struct passenger
{
    float time;
    float fromFloor;
    float toFloor;
};


int _tmain(int argc, _TCHAR* argv[])
{
    //need to instanciate elevators
    Elevator elevator; // test
    string STRING;
    string floor;
    string toFloor;
    ifstream infile;
    passenger p[25];
    infile.open("passenger requests.txt");

    if (!infile)
    {
        cout << "no good file failed! \n" << endl;
    }

    while (infile.good())
    {
        for (int i = 0; i < 25; ++i)
        {
            //getline(infile, STRING);
             // Saves the line in STRING.
            infile >> p[i].time;

            getline(infile, floor,',');
            infile >> p[i].fromFloor;
            getline(infile, toFloor, ',');
            infile >> p[i].toFloor;
            cout << p[i].time << "," << p[i].fromFloor << "," << p[i].toFloor <<  endl;


        }




}

infile.close();

system("pause");
return 0;

}

0 个答案:

没有答案