Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextStream - No data in File
QtWS25 Last Chance

QTextStream - No data in File

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 644 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Settz
    wrote on last edited by VRonin
    #1

    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;
    }
    
    JonBJ jsulmJ 2 Replies Last reply
    0
    • S Settz

      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;
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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?

      1 Reply Last reply
      2
      • S Settz

        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;
        }
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @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().

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        3
        • S Offline
          S Offline
          Settz
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            S 1 Reply Last reply
            2
            • Christian EhrlicherC Christian Ehrlicher

              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.

              S Offline
              S Offline
              Settz
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Then I would guess you overwrite it somewhere else.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                1
                • S Offline
                  S Offline
                  Settz
                  wrote on last edited by
                  #8

                  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 !

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved