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

Clean File

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 2.2k 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.
  • B Offline
    B Offline
    Bruno10x
    wrote on last edited by
    #1

    Hi everyone, I have a question, do I have a file like I do when I click a button to clean it all?

    QFile file("txt.txt");
                if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
                       return;
                file.close();
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to the forums.
      Im not 100% sure i understand what you ask.
      You want to erase a file when you click the button or do you ask
      if it will create an empty file, from the code shown?

      B 1 Reply Last reply
      2
      • mrjjM mrjj

        Hi and welcome to the forums.
        Im not 100% sure i understand what you ask.
        You want to erase a file when you click the button or do you ask
        if it will create an empty file, from the code shown?

        B Offline
        B Offline
        Bruno10x
        wrote on last edited by
        #3

        @mrjj Yes , I want erase a file when I click in a button.

        mrjjM 1 Reply Last reply
        0
        • B Bruno10x

          @mrjj Yes , I want erase a file when I click in a button.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Bruno10x
          Hi
          If you mean erase like in delete it, there is
          http://doc.qt.io/qt-5/qfile.html#remove-1

          Notice this is a static function
          so you just call it like

          QFile::remove( fullpathtofile.txt);

          you dont need a QFile variable.

          B 1 Reply Last reply
          4
          • mrjjM mrjj

            @Bruno10x
            Hi
            If you mean erase like in delete it, there is
            http://doc.qt.io/qt-5/qfile.html#remove-1

            Notice this is a static function
            so you just call it like

            QFile::remove( fullpathtofile.txt);

            you dont need a QFile variable.

            B Offline
            B Offline
            Bruno10x
            wrote on last edited by
            #5

            @mrjj Ok, if I just want erase the content in file, How I do?

            Christian EhrlicherC 1 Reply Last reply
            0
            • B Bruno10x

              @mrjj Ok, if I just want erase the content in file, How I do?

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Bruno10x said in Clean File:

              Ok, if I just want erase the content in file, How I do?

              Open the file, read the fileSize and then do a QFile::write(QByteArray(0, filesize))

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

              JonBJ 1 Reply Last reply
              5
              • Christian EhrlicherC Christian Ehrlicher

                @Bruno10x said in Clean File:

                Ok, if I just want erase the content in file, How I do?

                Open the file, read the fileSize and then do a QFile::write(QByteArray(0, filesize))

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @Christian-Ehrlicher

                You may have taken @Bruno10x's

                Ok, if I just want erase the content in file,

                a bit too literally :)

                For his benefit: if by "erase content" you mean keep it same size but fill it with all bytes of value 0 then @Christian-Ehrlicher's solution is what you need. [Clarification: provided you know your file is not too large... (else do it in a loop with a buffer's-worth at a time)]

                If you mean reset it to have length of zero, then your existing file.open(QIODevice::WriteOnly) should already be accomplishing that? [Clarification: http://doc.qt.io/qt-5/qiodevice.html#OpenModeFlag-enum

                QIODevice::WriteOnly 0x0002 The device is open for writing. Note that this mode implies Truncate.

                ]

                1 Reply Last reply
                5

                • Login

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