[solved] QTextstream problems



  • in header, QFile *file_contenente_elencoParole; QTextStream *leggi_scrivi;

    @
    Gestore::Gestore(QObject *parent) :
    QObject(parent)
    {
    QDir dir;
    dir.mkpath("C:/WoerterErinnerung");
    file_contenente_elencoParole = new QFile("C:/WoerterErinnerung/elencoParoleSalvate.txt");

    if(file_contenente_elencoParole->open(QFile::Text | QFile::ReadWrite | QFile::Append))
    {
        leggi_scrivi = new QTextStream(file_contenente_elencoParole);
        leggi_scrivi->setCodec("UTF-8");
    }
    leggi_scrivi.operator <<("NEW LINE THAT LEGGI_SCRIVI CAN NOT WRITE....");
    

    }
    @

    why leggi_scrivi do not write into the file?



  • You're

    leaking memory

    not flushing the QTextStream

    not closing the QFile



  • bq. in header, QFile *file_contenente_elencoParole; QTextStream *leggi_scrivi;

    How do you justify the usage of pointers here (except for leaking memory :D) ?



  • Why do you use pointers here?
    I would prefer using local stack variables:

    @
    Gestore::Gestore(QObject *parent) :
    QObject(parent)
    {
    QDir dir;
    dir.mkpath("C:/WoerterErinnerung");
    QFile file_contenente_elencoParol("C:/WoerterErinnerung/elencoParoleSalvate.txt");

    if(file_contenente_elencoParole.open(QFile::Text | QFile::ReadWrite | QFile::Append))
    {
        QTextStream leggi_scrivi(&file_contenente_elencoParole);
        leggi_scrivi.setCodec("UTF-8");
        leggi_scrivi  << "NEW LINE THAT LEGGI_SCRIVI CAN NOT WRITE....";
    }
    

    }
    @



  • i would to use leggi_scrivi as a global variable...



  • Global? uähhh....
    perhaps as member in a class, but I would not use globaö variiables if possible.

    But you could call "QTextStream::flush":http://doc.qt.nokia.com/4.7/qtextstream.html#flush



  • If you are using [[Doc:QTextStream]] then please do not set the QFile::Text flag, that's useful for plain files only.

    Also, you should use QIODevice::xxx flags, as those are defined in [[Doc:QIODevice]] not in [[Doc:QFile]].

    This works:

    @
    QFile file("/tmp/qdn-test.txt");
    if(file.open(QIODevice::WriteOnly | QIODevice::Append)) {
    QTextStream ts(&file);
    ts.setCodec("UTF-8");
    ts << "Test for DevNet Users" << "\n";
    ts << "more text" << "\n";
    file.close();
    }
    @

    The file contents are likely to be not written to the disk until the file is closed, so you might see some leaks.



  • ok, also why this snippet dows not work?
    @
    QFile file_contenente_elencoParole("C:/WoerterErinnerung/elencoParoleSalvate.txt");
    if(file_contenente_elencoParole.open(QIODevice::ReadWrite | QIODevice::Append))
    {
    QTextStream leggi_scrivi(&file_contenente_elencoParole);
    leggi_scrivi.setCodec("UTF-8");
    leggi_scrivi << "prova" << endl;
    qDebug() << leggi_scrivi.readLine();
    }
    @



  • Can you define "does not work"? No file is created? The file is created but it's empty? The contents are different from the expected ones?



  • QTextStream writes but does not read...
    qDebug() << leggi_scrivi.readLine();
    returns "".



  • I guess you're at the end of the device. Thus you get an empty string when trying to read.

    I would call @leggi_scrivi.seek(0)@ before



  • thank you!!!! you are simply a wizard!


Log in to reply
 

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