Robust Reading a text file



  • Greetings.

    I'm reading a text file with QFile and QTextStream and although I do not generate any error the testing I've done, I want my code is robust and handles any errors that may appear during reading (for example, that in the input file has less data than expected).

    The problem is that not exactly how to do that (make the code is sufficiently robust to signal that an error occurred during the reading), or rather do not know if I'm doing okay so far.

    What I'm doing is (roughly) the following:

    @
    QFile arch("inputFile.in");

    if( !arch.open( QIODevice::ReadOnly | QIODevice::Text ) )
    {
        //Give appropriate error message and exit
    }
    
    
    QTextStream entrada( &arch );
    while( (entrada.status() == QTextStream::Ok) && !entrada.atEnd() )
    {
        //Read the input data
        entrada >> dat1 >> dat2;
        ...
        entrada >> datN;
    
        //Process the data read...
    }
    
    
    if( entrada.status() != QTextStream::Ok )
    {
        //Give appropriate error message and exit
    }
    

    @

    Is it right or what I'm doing works?.
    Is it efficient or is the best way?.

    Thanks in advance for any responses and/or comments.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.