您好我正在做一些项目来读取csv文件的输入,我从文件中提取的值是Day,Month,Year,Hour,Minute,Windspeed,SolarRadiation。
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,因此它可以进入上个月。
**编辑::完整代码
**编辑::感谢您输入的人,我只是重写了我的代码的abit,我将循环参数更改为while(!actualFile.eof())
,而if语句我将if(day!=dayTemp || actualFile.eof())
和getline更改为while循环
对不清楚的问题和标题抱歉
答案 0 :(得分:1)
这里看一下:
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()
就足够了。
答案 1 :(得分:0)
你可以这样做:
声明变量bool reach;
getline(file, line);
if(file.eof()) reach = true;