Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QFile write/replace

    General and Desktop
    4
    15
    13012
    Loading More Posts
    • 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
      slinavipuz last edited by

      Hello world,

      I'm developing an app and its design requires me to edit particular line in file.

      creating new file is not an option.

      My file is formatted like this

      First line - 4characters\n
      Every other line- 64characters\n

      Therefore I always know the length of line, and I need to edit the first 4 characters, and I have no idea how to do that. Could anyone help me?

      EDIT: i have 1.5 hours to implement that, if not I have to wait another 24hrs (i can test that only at 00:00 local time) so forgive me if I failed to search forum thoroughly.

      1 Reply Last reply Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators last edited by

        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 Reply Quote 0
        • S
          slinavipuz last edited by

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

          1 Reply Last reply Reply Quote 0
          • A
            andre last edited by

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

            1 Reply Last reply Reply Quote 0
            • S
              slinavipuz last edited by

              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 Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                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 Reply Quote 0
                • S
                  slinavipuz last edited by

                  @ 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 Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    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 Reply Quote 0
                    • S
                      slinavipuz last edited by

                      [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 Reply Quote 0
                      • SGaist
                        SGaist Lifetime Qt Champion last edited by

                        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 Reply Quote 0
                        • S
                          slinavipuz last edited by

                          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 Reply Quote 0
                          • SGaist
                            SGaist Lifetime Qt Champion last edited by

                            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 Reply Quote 0
                            • S
                              slinavipuz last edited by

                              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 Reply Quote 0
                              • SGaist
                                SGaist Lifetime Qt Champion last edited by

                                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 Reply Quote 0
                                • S
                                  slinavipuz last edited by

                                  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 Reply Quote 0
                                  • First post
                                    Last post