【发布时间】:2013-03-25 21:46:43
【问题描述】:
我一直在开发一个 C++ 程序,在该程序中我读取文件内容然后复制到另一个文件,但它似乎总是跳过第一行。我见过其他人对此有问题,他们使用了这些代码行:
file.clear();
file.seekg(0);
重置位置,但它对我不起作用。我已经在多个地方尝试过,但仍然没有运气。有任何想法吗?这是我的代码。
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;
【问题讨论】:
-
尝试打印出包含的行。您将看到文件的第一行。你不能读两遍。 :)
-
我假设您在此代码块之外的某处声明了一堆变量...
-
您用
getline阅读了第一行,然后从不使用它。到那时,溪流已经过去了。 -
开始习惯将 for 循环写成更惯用的 for(int i = 0; i
-
@Tomas 是的,这只是代码的一部分
标签: c++ reset ifstream getline