C++ - while(getline)没有得到第一行文件

Aru*_*bix 1 c++ reset getline ifstream

我一直在研究一个C++程序,在那里我读取文件内容然后复制到另一个文件,但它似乎总是跳过第一行.我见过其他人遇到麻烦,他们使用了这些代码行:

file.clear();
file.seekg(0);
Run Code Online (Sandbox Code Playgroud)

重置位置,但它不适合我.我已经在多个地方尝试过,但仍然没有运气.有任何想法吗?继承我的代码.

ofstream write("Mar 23 2013.txt");

for(int x = 1; x <= 50; x++){

    stringstream ss;
    ss << "MAR23_" << x;

    ifstream file(ss.str().c_str());

    if(!file.is_open())
        cout << ss.str() << " could not be opened/found." << endl;
    else{  

        while(getline(file,line)){

            file >> time >> ch >> sensor1 >> ch >> temp >> ch >> 
                    sensor2 >> ch >> sensor3;

            file.ignore(numeric_limits<streamsize>::max(), '\n');

            //output = convertEpoch(time);

            write << time << "  Temperature:" << temp << "ºF  S1:" <<
                        sensor1 << "  S2:" << sensor2 << "  S3:" << 
                        sensor3 << endl;

        }
        file.close();
    }  
}

write.close();

return 0;
Run Code Online (Sandbox Code Playgroud)

小智 9

你错过了第一行,因为你读到了它line.事实上,你应该失去的不仅仅是第一行.

从文件中读取后,使用字符串流.

while (std::getline(infile, line))
{
    std::istringstream iss(line);
    iss>> time >> ch >> sensor1 >> ch >> temp >> ch >> 
                    sensor2 >> ch >> sensor3;
// ...


}
Run Code Online (Sandbox Code Playgroud)