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 write/replace

QFile write/replace

Scheduled Pinned Locked Moved General and Desktop
15 Posts 4 Posters 13.9k 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.
  • Chris KawaC Offline
    Chris KawaC Offline
    Chris Kawa
    Lifetime Qt Champion
    wrote on last edited by
    #2

    You need to open file in write and append(this might be misleading a little) mode and then seek to the beginning:
    @
    QFile f("c:/some.file");
    if(f.open(QFile::WriteOnly | QFile::Append))
    {
    f.seek(0);
    f.write("test");
    f.close();
    }
    @

    1 Reply Last reply
    0
    • S Offline
      S Offline
      slinavipuz
      wrote on last edited by
      #3

      o.O this is the same as C... I feel stupid....

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #4

        Or, you memmap the file and just manipulate the data as if you were doing it in-memory.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          slinavipuz
          wrote on last edited by
          #5

          Me again... This is not quite working for me...

          so... my file is basically a 'database' and first line is number of lines after that...
          when inputing new line to file, i do following:
          -append line with QTextStream
          -seeking to begining of file and changing number to +1

          So my problem is, seek(0) does nothing. new number is apended nevertheless... should i open file only in write mode?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #6

            What do you use for seeking, QFile or QTextStream ? They might be interfering one with the other

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • S Offline
              S Offline
              slinavipuz
              wrote on last edited by
              #7

              @ if(h_fajl.open(QFile::WriteOnly | QFile::Append))
              {
              h_fajl.seek(0);
              h_fajl.write(p.toAscii()+"\n");
              h_fajl.close();
              }@

              p is a Qstring....
              Oh and I thought that some interference could happen so i closed file, and reopened here.... still same thing...

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #8

                Did you also do a textStream.setDevice(0) ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  slinavipuz
                  wrote on last edited by
                  #9

                  [quote author="SGaist" date="1361198821"]Did you also do a textStream.setDevice(0) ?[/quote]

                  no. what should that be? Im not aware what that method even does... :/

                  My issues are with seek...

                  when i want to append to file i do
                  @ h_out << hString+"\n";@

                  and it works flawlesly. But when i do the
                  @ if(h_fajl.open(QFile::WriteOnly | QFile::Append))
                  {
                  h_fajl.seek(0);
                  h_fajl.write(p.toAscii()+"\n");
                  h_fajl.close();
                  }
                  @
                  this ALSO doing appending and not replacing first chars of file

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    That will remove your file from the text stream.

                    You open it with the Append flag, so it will append.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      slinavipuz
                      wrote on last edited by
                      #11

                      well Krzysztof said that should be done like that (with both append and write flags).
                      ok, ill try to remove file from stream... holding my fingers crossed...

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        He's right: you need Append when you want to append (your first step) but then you want to overwrite the content of your data (second step)

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          slinavipuz
                          wrote on last edited by
                          #13

                          writting without append overwrites everything in my file. not only first chars. I want other thing in file to be intact...

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #14

                            Try the equivalent of this:

                            @QFile testFile("test.txt");
                            testFile.open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text);
                            testFile.write("test\n");
                            testFile.close();
                            testFile.open(QIODevice::ReadWrite|QIODevice::Text);
                            testFile.seek(0);
                            testFile.write("1\n"); @

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              slinavipuz
                              wrote on last edited by
                              #15

                              i think i tried something that should be the exact equivalent of that. And it apended text.

                              Right now, i am quite confused. As far as I know computer does not have 'free will'. and one piece of code (i had it saved) erased whole file and started writing, and now it appends (and append flag is not turned on).

                              So now i need to find diferences between my codes, and see where did i go wrong. After that I will try to write your code, and see what's the output. Right now im confused as hell and not even sure what the hell is going on with my codes :) oh, and i even managed to get segfault with wrong qdebug call. maybe it's time to go to sleep :/

                              EDIT 1.
                              I made it. I held my first row in separated file, as far as memory weight of my program, it will be the same if not smaller. It will be little harder to implement reading but nothing will be to hard.

                              So now i have to do some backups, and to deploy changes, and ofc to sleep. Tomorrow I'll try your code, so I'll post results for someone to see in future :)

                              Still I would like an explanation about one thing. I feel like I'm missing something obvious.
                              If I used qTextStream to output somethings to my QFile that is opened with ReadWrite flag, strange thing happened. It didn't overwrite, it didn't append. It did something that looked like those two combined. I don't have time to even try to understand what was going on at that moment.

                              So i used write() method and every time i seek(0) before it, so it is a success...
                              When i find out how to edit only one line in file, il post it here :)

                              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