I am trying to make my program read in data from a data file (.dat) (which is actually just a text file). Therefore, of course, I use a loop condition while(!file.eof()), but it never returns true. Here is my function:
void Table::readIn(const char finput[]){
std::ifstream file;
file.open(finput);
if (!file.is_open())
{
std::cout << "Cannot open " << finput << std::endl;
return;
}
char key[100];
file.get(key, 99, '\n');
while (!file.eof())
{
stock * item = new stock;
item->setTick(key);
file.get(key, 99, '\n');
item->setName(key);
file.get(key, 99, '\n');
item->setValue(atof(key));
file.get(key, 99, '\n');
item->setDate(key);
file.get(key, 99, '\n');
item->setYearReturn(atof(key));
file.get(key, 99, '\n');
addStock(item);
}
}
and here is what is in my data file:
TSLA
Tesla Motors, Inc.
30160000000
November 6, 2015
13.1
I would like to give you more information, but the fact that the program cycles through while (!file.eof())indefinitely is all I know about this problem.
: while. , get() ( while) key , get() key "". , , '\n' . , , , ?
2: : iostream:: eof ?, while.