[英]How to know if next getline is EOF C++
嗨,我正在做一些项目来从 csv 文件中读取输入,我从文件中提取的值是日、月、年、小时、分钟、风速、太阳能辐射。
bool readFile(MasterMap &mastMap){
const string index = "MetData/data.txt"; //<--- container of every file name constant cant change
string temp, temp2, token; // <--temp to store filename from index,
// <-- temp2 to getline from every single line from file
// << token to store temporary data
ifstream myFile(index.c_str()); // create ifstream to read into the index file
if(myFile.is_open()){
while(getline(myFile, temp))
{
cout << temp << endl; // <--- Print out the file name (testing)
string fileName = "MetData/"+temp; // <-- filename is to contain temp
ifstream actualFile(fileName.c_str()); // <--- Open the File
if(actualFile.is_open())
{
time timeValue;
MonthsMap monthMap;
windLog wLog;
solarRadiation SR;
windSpeed WS;
string MonthYear = "";
int MY = 0;
int day = 0;
int totalCount = 0;
getline(actualFile, temp2); //remove the first line of the file.
while(**actualFile.eof()**)
{
getline(actualFile, temp2)
stringstream ss(temp2); // <-- pas line to stream
getline(ss, token,'/'); // <--Token for day;
int dayTemp = atoi(token.c_str());
getline(ss, token,'/'); // <--Token for month;
string month = token.c_str();
getline(ss, token,' '); // <--Token for year;
string year = token.c_str();
MonthYear = month + year;
if(MY == 0 && day == 0){
MY = atoi(MonthYear.c_str());
day = dayTemp;
}
if(day!=dayTemp || actualFile.eof()){
monthMap.insertWindLog(day, wLog);
wLog = windLog();
day = dayTemp;
if(MY!=atoi(MonthYear.c_str()) || actualFile.eof()){
cout << "Entered Month > " << MY << endl;
mastMap.insertData(MY, monthMap);
monthMap = MonthsMap();
MY=atoi(MonthYear.c_str());
}
getline(ss, token,':'); // <-- Token for Hour;
timeValue.setHour(atoi(token.c_str()));
getline(ss, token,','); // <-- Token for Minute;
timeValue.setMinute(atoi(token.c_str()));
for(int i = 0; i<9; i++)
getline(ss, token, ',');
getline(ss, token,','); // <-- Token for WindSpeed
WS.setTime(timeValue);
WS.setWindspeed(atof(token.c_str()));
getline(ss,token,','); // <-- Token for Solar Radiation
SR.setTime(timeValue);
SR.setSolarRadiation(atof(token.c_str()));
wLog.insertDataSR(SR);
wLog.insertDataWS(WS);
}
else{
getline(ss, token,':'); // <-- Token for Hour;
timeValue.setHour(atoi(token.c_str()));
getline(ss, token,','); // <-- Token for Minute;
timeValue.setMinute(atoi(token.c_str()));
for(int i = 0; i<9; i++)
getline(ss, token, ',');
getline(ss, token,','); // <-- Token for WindSpeed
WS.setTime(timeValue);
WS.setWindspeed(atof(token.c_str()));
getline(ss,token,','); // <-- Token for Solar Radiation
SR.setTime(timeValue);
SR.setSolarRadiation(atof(token.c_str()));
wLog.insertDataSR(SR);
wLog.insertDataWS(WS);
}
totalCount++;
}
cout << totalCount << endl;
actualFile.close();
}else
return false;
}
myFile.close();
}else
return false;}
问题问题是当行到达文件末尾时,它将立即结束循环,但我需要检查当前行是否为EOF,以便它可以进入上个月。
**已编辑 :: 完整代码
**已编辑 :: 谢谢你们的输入,我只是重写了我的代码,我将循环参数更改为while(!actualFile.eof())
和 if 语句我把if(day!=dayTemp || actualFile.eof())
并且 getline 在 while 循环之后
对不起,不清楚的问题和标题
看看这里:
http://www.cplusplus.com/reference/istream/istream/getline/
我引用:
如果到达文件结尾,该函数还将停止提取字符。 如果过早到达(在写入 n 个字符或找到 delim 之前),该函数将设置eofbit标志。
更多信息在这里:
http://www.cplusplus.com/reference/ios/ios_base/iostate/
你想看看rdstate
:
http://www.cplusplus.com/reference/ios/ios/rdstate/
并测试标志eofbit
。
该资源还向您展示了eof()
:
http://www.cplusplus.com/reference/ios/ios/eof/
它举了一个例子:
// ios::eof example
#include <iostream> // std::cout
#include <fstream> // std::ifstream
int main () {
std::ifstream is("example.txt"); // open file
char c;
while (is.get(c)) // loop getting single characters
std::cout << c;
if (is.eof()) // check for EOF
std::cout << "[EoF reached]\n";
else
std::cout << "[error reading]\n";
is.close(); // close file
return 0;
}
因此,使用eof()
来测试您是否已到达文件末尾。 但是,如果适合您的情况,您也可以进行该标志测试。 我认为eof()
就足够了。
你可以这样做:Declare variable bool reach;
getline(file, line);
if(file.eof()) reach = true;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.