Why does eof () never return?

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.

+4
2

std::istream::get , . , , , .

, std::istream::getline ( ) std::getline std::string, .


eof, , ( ) , , ,

if (!std::getline(...))
    // end of file or error
+3

while (!file.eof())

while (true)
    {
        /*code*/
        file.get(key, 99, '\n');
        if (file.eof()) { break; } // and similarly for other usages where you are reading the stream
        /*code*/
    }

: . , , ifstream:: get:

c delim, Traits:: eq (c, delim).

get . get , , . , , getline

0

Source: https://habr.com/ru/post/1614832/


All Articles