[英]ifstream still reading after eof
I am writing a program that is going to have to import data from a file into various containers. 我正在编写一个程序,该程序必须将文件中的数据导入各种容器。 I have it importing everything properly, but it is continuing to read after what is supposed to be the eof.
我可以正确导入所有内容,但是在应该被认为是eof之后,它仍在继续阅读。 I have a feeling I am not properly telling the loop when to end, but the code is below for everyone to look at.
我感觉我没有正确告诉循环何时结束,但是下面的代码供大家查看。
bool InitLoad(vector<string>&num, vector<string>&name, vector<double>&price, vector<char>&tax)
{
ifstream invFile;
int intTemp;
string strTemp;
double dubTemp;
char chTemp;
string fileLoc = "C:/Users/owner/Documents/Visual Studio 2010/Projects/CISS 350/Week 1 Grocery Register/Week 1 Grocery Register/Invent.dat";
//Open Invent.dat file. Location below is the location used on creators computer. Other may need to modify file location
invFile.open(fileLoc.c_str(), ios::in);
//If Invent.dat file fails to open display error message and return false
if(invFile.fail())
{
cout << "Could not open inventory file" << endl;
return false;
}
if(invFile)
{
//Read first line of the file
getline(invFile, strTemp, ' ');
while(invFile) //while invFile contains data display import the list
{
cout << strTemp << " ";
num.push_back(strTemp);
getline(invFile, strTemp, ' ');
cout << strTemp << " ";
name.push_back(strTemp);
getline(invFile, strTemp, ' ');
dubTemp = atof(strTemp.c_str());
cout << dubTemp << " ";
price.push_back(dubTemp);
invFile.get(chTemp);
cout << chTemp;
tax.push_back(chTemp);
getline(invFile, strTemp, ' ');
}
}
invFile.close();
cout << endl;
//Verify Proper input...REMOVE WHEN COMPLETE
cout << "Verifying input data correct..." << endl;
int vecSize = num.size();
cout << vecSize << endl;
for(int i = 0; i < vecSize; i++)
{
cout << num[i] << " " << name[i] << " " << price[i] << " " << tax[i] << endl;
}
}
Your check does not check eof flag http://www.cplusplus.com/reference/ios/ios/operator_bool/ 您的支票未检查eof标志http://www.cplusplus.com/reference/ios/ios/operator_bool/
use invFile.eof()
instead 使用
invFile.eof()
代替
And also eof flag would be setted after reading past EOF 并且在读取过去的EOF 之后也会设置eof标志
PS: OMG!! PS:天哪! do not use
atof
, just do invFile << dubTemp
不使用
atof
,只做invFile << dubTemp
Since your data is space separated you can uses formatted input instead of getline() on every string. 由于数据是用空格分隔的,因此可以在每个字符串上使用格式化的输入,而不是使用getline()。 Something along the lines of this.
与此类似。
string lineTemp;
while(getline(invFile, lineTemp)) //while invFile contains data display import the list
{
string strTemp1, strTemp1, dubTemp, chTemp;
istringstream lstr(lineTemp);
if(lstr >> strTemp1 >> strTemp2 >> dubTemp >> chTemp) {
num.push_back(strTemp1);
name.push_back(strTemp2);
price.push_back(dubTemp);
tax.push_back(chTemp);
cout << strTemp1 << " "
<< strTemp2 << " "
<< dubTemp << " "
<< chTemp << endl;
}
else {
// Something is wrong with the line format.
}
}
This will read the data in a formated way and to the proper type. 这将以格式化的方式和正确的类型读取数据。 Plus you don't have to worry about empty lines or extra characters in lines.
另外,您不必担心空行或行中多余的字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.