Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTextStream - No data in File



  • Hi All

    I am having problems with writing data to a text file using QTextStream.

    The file is created but has nothing in it.

    I am missing something so obvious.

    Thanks in advance.

    Code snippets below.

    int cTask::processNewTransactions(QString accName, QString tmpFileName, QString transFileName, vector<cCategory> &catList)
    {
        int retVal = OK;
        QFile tFile(transFileName);
        QFile oFile(tmpFileName);
        QString line;
        string  token;
        int tokenId = 0;
        bool bHeaderRow = true;
        cTransaction objTrans;
        std::string::size_type sz = 0;     // alias of size_t
    
        if(!tFile.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            cout << "ERROR: Unable to open Transactions File\n";
    
            emit finished();
        }
    
        if(!oFile.open(QIODevice::WriteOnly | QIODevice::Text))
        {
            cout << "ERROR: Unable to open Work File\n";
    
            emit finished();
        }
    
        QTextStream outFile(&oFile);
        
        // Write header to output file
        outFile << QString("Account,")
                << QString("Date,")
                << QString("ID,")
                << QString("Spare,")
                << QString("Type,")
                << QString("Text,")
                << QString("Amount,")
                << QString("Balance,")
                << QString("Group,")
                << QString("Sub Group,")
                << QString("Classification")
                << endl;
                
    
        transModel->clearData();
    
        while (!tFile.atEnd())
        {
            line = tFile.readLine();
    
            if(!bHeaderRow || bNoHeaderRow)
            {
                std::istringstream iss(line.toStdString());
    
                tokenId = 0;
    
                objTrans.txAccName = accName;
    
     ——— Snip
    
    Code to built objTrans
    
     ——— Snip
    
     
                updateCategory(objTrans, catList);
                //cout << objTrans; // << endl;
                outFile << objTrans << endl;
                transModel->addRow(objTrans);
    
            }
            bHeaderRow = false;
        }
    
        outFile.flush();
        oFile.close();
        tFile.close();
    
        return retVal;
    }
    


  • @Settz said in QTextStream - No data in File:

    The file is created but has nothing in it.

    At a glance code looks OK (you obviously got it from some very old post here?). You won't see anything in it necessarily till after outFile.flush(); oFile.close();, are you looking in the file before then?


  • Lifetime Qt Champion

    @Settz You should also add QIODevice::Append to open() call if you do not want to lose old file content each time you call processNewTransactions().



  • Thx @JonB

    I am checking the file after the application has run. It's driving me nuts lol

    In other areas of the app I am opening and writing data to various files .

    Thx @jsulm

    This is a temporary workfile and a new one is created each time the app runs - no need to append.


  • Lifetime Qt Champion

    So simply open the file, write something into it and see if there is content in it without all the other loops directly at the start of your function.



  • @Christian-Ehrlicher Thankyou - good idea - did that but nothing


  • Lifetime Qt Champion

    Then I would guess you overwrite it somewhere else.



  • Thankyou all for your help - it is working now.

    @Christian-Ehrlicher You put me on the right track.

    In another part of the application I had a bug that would reopen the file for writing !


Log in to reply