Qtextstream in(QFile) does not read



  • hi. this is my code:
    @
    if(pathInput->open(QIODevice::ReadOnly | QIODevice::Text))
    {
    QTextStream in(pathInput);

        if(pathOutput->open(QIODevice::Text | QIODevice::WriteOnly))
        {
            QTextStream out(pathOutput);
    
            if(in.readAll() != "")
            {
                qDebug() << in.readLine();
                out << "ciao";
            }
            else
            {
                QMessageBox alert;
                alert.setText("file vuoto");
                alert.setWindowTitle("Attenzione");;
                alert.resize(200,200);
                alert.move(500,500);
                alert.setIcon(QMessageBox::Critical);
                alert.exec&#40;&#41;;
            }
    
            pathInput->close(&#41;;
            pathOutput->close();
        }
        else
        {
            QMessageBox alert;
            alert.setIcon(QMessageBox::Critical);
            alert.adjustSize();
            alert.move(500,500);
            alert.setWindowTitle("File di output");
            alert.setText("Problemi con il file di output. Provare a riavviare l'applicazione come amministratore!");
        }
    }
    else
    {
        QMessageBox alert;
        alert.adjustSize();
        alert.move(500,500);
        alert.setText("Problemi nell'apertura del file di input. Riavviare l'applicazione!\n\nSoluzioni:\n1)Eseguire il programma come amministratore;\n2)Verficare la formattazione del file &#40;file non di testo, ecc.&#41;"&#41;;
        alert.setWindowTitle("File di input");
        alert.setIcon(QMessageBox::Critical);
    }
    

    @

    why can not i read from in(QTextStream)? in.readAll(); returns ""... =(

    [edit: code highlight fixed / Denis Kormalev]


  • Moderators

    Presumably your stream is empty.
    You need to check whether with "pathInput" is a problem. Maybe it is poiting towards an empty file.



  • readAll() returns the whole content of the stream and empties it.
    You should store the returned content to a variable. Something like this:

    QString data = in.readAll(); // content is copied to "data" and the input stream is emptied.



  • why does not the following code read from the input file?

    @
    if(pathInput->open(QFile::Text | QFile::ReadOnly))
    {
    QTextStream in (pathInput);
    if(in.readAll() != "")
    {
    if(pathOutput->open(QFile::Text | QFile::WriteOnly | QFile::Truncate))
    {
    QTextStream out (pathOutput);
    QString line = NULL;
    //programma vero e proprio: codice fondamentale
    do
    {
    line = in.readAll();
    qDebug() << line;
    }while(!line.isNull());
    //FINE programma vero e proprio: codice fondamentale
    }
    }
    }
    @

    pathInput and pathOutput are QFile

    [[Merged this back into the original thread, Tobias]]
    [EDIT: code formatting, pleas wrap in @-tags, Volker]


  • Moderators

    did you try what tdmatsu suggested in "your other post":http://developer.qt.nokia.com/forums/viewthread/6349/

    try to use
    @QString strData = in.readAll() @
    and look what's in it :
    @qDebug() << "strData : " << strData;@

    is strData empty?


  • Moderators

    You can not read from a file you have opened as QFile::WriteOnly before.

    Plus the file will be empty anyway after truncating it.



  • yes... i beg your pardon! i did not realize it!
    what a mess! and now what should i do?

    so, tdmatsu said that QTextStream::readAll() reads all the content of the document, then the cursor is just at the end of the file!


  • Moderators

    Read the documentation (you have all the pieces, so you know which docs to read) and then fix your code.



  • you scare me, troll! :D



  • readAll() is not for checking the validity of the stream, it's for taking out (or consuming) all the data from input. that's why you have to store the returned data to a variable.

    if you are trying to process the text line by line, probably readLine() is what you want:

    http://doc.qt.nokia.com/latest/qtextstream.html#readLine

    use it in a loop with proper end of data checking.


Log in to reply
 

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