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. Truncate a file under mac with Qt
QtWS25 Last Chance

Truncate a file under mac with Qt

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 4.7k 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.
  • L Offline
    L Offline
    luca72
    wrote on last edited by
    #1

    Hello under linux if i have tru truncate a file i use :
    truncate "file.xxx" --size mysize

    now i'm under mac and the truncate don't work, i can do it using Qt?

    Thanks

    Luca

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jupiter
      wrote on last edited by
      #2

      @QFile f(<Path to File>);
      if (f.open(QIODevice::Truncate | QIODevice::WriteOnly)) {
      //f is now empty and opened for writing
      }@

      1 Reply Last reply
      0
      • L Offline
        L Offline
        luca72
        wrote on last edited by
        #3

        but where i specify the file size.

        Luca

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jupiter
          wrote on last edited by
          #4

          truncate always clears everything, like in SQL Truncate table also removes all rows.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dangelog
            wrote on last edited by
            #5

            Have you tried to open the QFile docs and look for "truncate"?

            Software Engineer
            KDAB (UK) Ltd., a KDAB Group company

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              "QFile::resize() ":http://doc.qt.nokia.com/4.7/qfile.html#resize (or its static equivalent) should be your friend.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • L Offline
                L Offline
                luca72
                wrote on last edited by
                #7

                Just for info the file must be open in read mode o only in write or in read and write?
                Thanks
                Luca

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  From a quick look into the sources, it seems that it doesn't matter whether the file is open.

                  To be on the safe side: try yourself - which would lead to the quickest answer, instead of waiting for a forum reply, BTW :-)

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    luca72
                    wrote on last edited by
                    #9

                    Hello again
                    i have write this :
                    @QFileInfo info_file(myfile);
                    long long lgqt = info_file.size();
                    QFile mfile(nome_f);
                    @
                    if i insert a breack point when o try to go to the line :
                    long long lgqt = info_file.size(); i get the error :
                    cannot continue debugged process:
                    Warning:
                    cannot inster breackpoint -101
                    Error accessing memory address xxxxxxx input/output error.
                    But if i insert the breackpoint for example on the line "QFile mfile(nome_f);" e get no error and also i get the file size
                    Why?

                    Luca

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on last edited by
                      #10

                      The code is basically ok. It seems that it is broken otherwise.

                      The snippet lacks complete information, though. We're not able to analyze it otherwise.

                      Of what type is myfile? Where is it defined?

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      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