Frage

I've got a little problem. I want to skip some lines in a file. Atm I am using

        for(int i=0; i < ln -1; ++i)
        {
            ifFile.ignore((std::numeric_limits<std::streamsize>::max)(),'\n');
        }

On cplusplus.com there is written that ignore will throw an exception if eof is reached. But I can't get it with try/catch. What did I do wrong? Or did I misread sth?

What I need is that I want to start at first line again for some operations if eof is reached.

So I tried sth but it didn't work:

        for(int i=0; i < ln -1; ++i)
        {
            try
            {
                ifFile.ignore((std::numeric_limits<std::streamsize>::max)(),'\n');
            }
            catch(exception e)
            {
                if(ifFile.eof())
                {
                    ifFile.clear(ifFile.eofbit);
                    ifFile.seekg(std::ios::beg);
                    _readCounter.currLine = 1;
                }
            }
        }

But it didn't run into catch().

If there are some questions feel free to ask.

Corrected one:

        for(int i=0; i < ln; i++)
        {
            if(ifFile.ignore((std::numeric_limits<std::streamsize>::max)(),'\n').eof())
            {
                ifFile.clear(ifFile.eofbit);
                ifFile.seekg(std::ios::beg);
            }
        }
War es hilfreich?

Lösung

Just check eof() after the call to ignore():

if (f.ignore(std::numeric_limits<std::streamsize>::max(), '\n').eof()) {
    // end of file was found
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top