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. QFile / QTextStream
Qt 6.11 is out! See what's new in the release blog

QFile / QTextStream

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.4k Views 1 Watching
  • 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.
  • ClaraCasteC Offline
    ClaraCasteC Offline
    ClaraCaste
    wrote on last edited by
    #1

    Hi everyone,

    I want to do a very simple thing but I can't do it ...
    I have a log file saved in some place (D://test.txt), I use a QTextStream to write text in it.

    m_file.setFileName(fileName);
    if (m_file.open(QIODevice::WriteOnly|QIODevice::Text))
    {
    	m_streamFile.setDevice(&m_file);
    }
    
    ...
    
    m_streamFile << msg;
    endl(m_streamFile);
    

    I create a menu to change its location.
    I just replace the file name:

    m_file.rename(dirName + "/" + fileName + ".txt");
    

    But when new log messages appear, they are not written in the new file ...

    Any idea ?

    Thanks !

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      before m_streamFile << msg; could you put Q_ASSERT(m_streamFile.device()->isOpen()); ?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • miclandM Offline
        miclandM Offline
        micland
        wrote on last edited by
        #3

        The API of QFile::rename(...) says:
        "The file is closed before it is renamed."
        So you have to open the file again after renaming...

        1 Reply Last reply
        0
        • ClaraCasteC Offline
          ClaraCasteC Offline
          ClaraCaste
          wrote on last edited by
          #4

          @VRonin said:

          Q_ASSERT(m_streamFile.device()->isOpen());

          You are right, the file is closed. But if I open it, the previous file content is cleared ...

          m_file.rename(newFileName);
          if (m_file.open(QIODevice::WriteOnly | QIODevice::Text))
          {
          	m_streamFile.setDevice(&m_file);
          }
          
          miclandM 1 Reply Last reply
          0
          • ClaraCasteC ClaraCaste

            @VRonin said:

            Q_ASSERT(m_streamFile.device()->isOpen());

            You are right, the file is closed. But if I open it, the previous file content is cleared ...

            m_file.rename(newFileName);
            if (m_file.open(QIODevice::WriteOnly | QIODevice::Text))
            {
            	m_streamFile.setDevice(&m_file);
            }
            
            miclandM Offline
            miclandM Offline
            micland
            wrote on last edited by
            #5

            @ClaraCaste Try to open in append mode, otherwise the file will be truncated:
            if (m_file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))

            1 Reply Last reply
            3
            • ClaraCasteC Offline
              ClaraCasteC Offline
              ClaraCaste
              wrote on last edited by
              #6

              It works !
              It seems obvious ...
              Thank you very much !

              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