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. change/add letter in text file c++
Forum Updated to NodeBB v4.3 + New Features

change/add letter in text file c++

Scheduled Pinned Locked Moved Unsolved General and Desktop
26 Posts 5 Posters 3.6k 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.
  • R RuWex

    @JonB
    that is the point!!
    I have a function that i am reading the file and something and every line i send to another function in order to check if it begins with # (the function get the line and pointer)
    and then I want that the function will continue with the fixes file
    its really important for me

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #10

    @RuWex said in change/add letter in text file c++:

    and then I want that the function will continue with the fixes file

    Don't know what this mean, but did you actually read what @Christian-Ehrlicher wrote?

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    R 1 Reply Last reply
    1
    • R Offline
      R Offline
      RuWex
      wrote on last edited by
      #11

      @jsulm what do you mean?
      how can I overwrite?
      that it the best for me

      jsulmJ 1 Reply Last reply
      0
      • R RuWex

        @JonB
        that is the point!!
        I have a function that i am reading the file and something and every line i send to another function in order to check if it begins with # (the function get the line and pointer)
        and then I want that the function will continue with the fixes file
        its really important for me

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

        @RuWex said in change/add letter in text file c++:

        its really important for me

        What is the relevance of this? We have told you what you need to do and that you cannot write back to the text file while reading from it. Just because something else might be "important" to you does not mean it can be done or you can ignore how things work/don't work!

        1 Reply Last reply
        1
        • jsulmJ jsulm

          @RuWex said in change/add letter in text file c++:

          and then I want that the function will continue with the fixes file

          Don't know what this mean, but did you actually read what @Christian-Ehrlicher wrote?

          R Offline
          R Offline
          RuWex
          wrote on last edited by
          #13

          @jsulm ya,
          but there are not a easier and nicer way?

          jsulmJ JonBJ 2 Replies Last reply
          0
          • R RuWex

            @jsulm what do you mean?
            how can I overwrite?
            that it the best for me

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #14

            @RuWex This is what @Christian-Ehrlicher wrote:
            "Read the file line by line and write the modified version out into a new file. Then close and remove the old file and rename the new one."
            Did you read it?!
            It was already explained in this thread that you CAN'T read AND write into the same file in a meaningful way. And it was also explained to you that you need to write to a temporary file first and rename it if done. Did you read that?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            R 1 Reply Last reply
            1
            • R RuWex

              @jsulm ya,
              but there are not a easier and nicer way?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #15

              @RuWex said in change/add letter in text file c++:

              but there are not a easier and nicer way?

              If the file is not too big you can keep the lines you read in memory until you're done with reading the file and modifying its content. Then write all this lines at once into the old file (after closing and reopening it for writing).

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • jsulmJ jsulm

                @RuWex This is what @Christian-Ehrlicher wrote:
                "Read the file line by line and write the modified version out into a new file. Then close and remove the old file and rename the new one."
                Did you read it?!
                It was already explained in this thread that you CAN'T read AND write into the same file in a meaningful way. And it was also explained to you that you need to write to a temporary file first and rename it if done. Did you read that?

                R Offline
                R Offline
                RuWex
                wrote on last edited by
                #16

                @jsulm ou I did not notice
                its great idea
                thank you!!

                1 Reply Last reply
                0
                • R RuWex

                  @jsulm ya,
                  but there are not a easier and nicer way?

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

                  @RuWex
                  If your file is small you could read it all into memory (splitting into lines for an array), make your changes in memory, and then overwrite the original file in one go. That would avoid the need for two files. But it does not alter the fact that you cannot write to/overwrite the file until you have finished reading from it completely....

                  R 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @RuWex
                    If your file is small you could read it all into memory (splitting into lines for an array), make your changes in memory, and then overwrite the original file in one go. That would avoid the need for two files. But it does not alter the fact that you cannot write to/overwrite the file until you have finished reading from it completely....

                    R Offline
                    R Offline
                    RuWex
                    wrote on last edited by
                    #18

                    @JonB but I can do it in te middle that the
                    readfile()- read a text fule and send to the changetextfile() function each line
                    so can I do it every time I found # IN CHANGETEXTFILE?

                    jsulmJ JonBJ 2 Replies Last reply
                    0
                    • R RuWex

                      @JonB but I can do it in te middle that the
                      readfile()- read a text fule and send to the changetextfile() function each line
                      so can I do it every time I found # IN CHANGETEXTFILE?

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #19

                      @RuWex You need to write ALL lines back to the file, not only lines you changed...

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • R RuWex

                        @JonB but I can do it in te middle that the
                        readfile()- read a text fule and send to the changetextfile() function each line
                        so can I do it every time I found # IN CHANGETEXTFILE?

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

                        @RuWex
                        I think we have all tried to answer as best we can.

                        You cannot update a text file "in place" or "as you go" or "only certain lines". You have to do the whole text file, from beginning to end. You can do that either by using a second file for writing as you read or by doing the work on an in-memory copy of the file and then writing it back.

                        R 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @RuWex
                          I think we have all tried to answer as best we can.

                          You cannot update a text file "in place" or "as you go" or "only certain lines". You have to do the whole text file, from beginning to end. You can do that either by using a second file for writing as you read or by doing the work on an in-memory copy of the file and then writing it back.

                          R Offline
                          R Offline
                          RuWex
                          wrote on last edited by
                          #21

                          @JonB YES, this is I understood
                          my question was if I can change ALL the file while the function readfile reading,
                          Imean every time that found # to change all file?

                          Christian EhrlicherC 1 Reply Last reply
                          0
                          • R RuWex

                            @JonB YES, this is I understood
                            my question was if I can change ALL the file while the function readfile reading,
                            Imean every time that found # to change all file?

                            Christian EhrlicherC Online
                            Christian EhrlicherC Online
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #22

                            @RuWex said in change/add letter in text file c++:

                            if I can change ALL the file while the function readfile reading,
                            Imean every time that found # to change all file?

                            Why not? It's up to you what you write back into the new file.

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

                            R 1 Reply Last reply
                            1
                            • Christian EhrlicherC Christian Ehrlicher

                              @RuWex said in change/add letter in text file c++:

                              if I can change ALL the file while the function readfile reading,
                              Imean every time that found # to change all file?

                              Why not? It's up to you what you write back into the new file.

                              R Offline
                              R Offline
                              RuWex
                              wrote on last edited by
                              #23

                              @Christian-Ehrlicher thank!!

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                SimonSchroeder
                                wrote on last edited by
                                #24

                                I'd like to disagree with the rest of you. Certainly, at the proficiency level of the OP he should just write to a new file like suggested. I am also not entirely sure if QFile supports this feature. However, with good old std::fstream (which does both input and output on the same file) it might work. Yes, you cannot insert something into the file. But flipping '#' to '' might actually work. BIG WARNING: This only work with plain old ASCII or Latin-1 (or similar) character encodings. With UTF-8 you have to be careful as hell and should not flip arbitrary characters ('#' and '' are in the ASCII range of UTF-8 and would be fine). std::fstream has two different pointers for reading and writing. Most likely you also have to make sure to open in binary mode.

                                My claims are fully untested, so I might also be mistaken about the details.

                                If there's anything in my description that is unfamiliar to you, then just go the way already suggested. There are so many sources of errors with the approach I described that you need a really good reason why you want to do it. And you should like the pain that comes with debugging this if it fails ;-)

                                JonBJ Christian EhrlicherC 2 Replies Last reply
                                1
                                • S SimonSchroeder

                                  I'd like to disagree with the rest of you. Certainly, at the proficiency level of the OP he should just write to a new file like suggested. I am also not entirely sure if QFile supports this feature. However, with good old std::fstream (which does both input and output on the same file) it might work. Yes, you cannot insert something into the file. But flipping '#' to '' might actually work. BIG WARNING: This only work with plain old ASCII or Latin-1 (or similar) character encodings. With UTF-8 you have to be careful as hell and should not flip arbitrary characters ('#' and '' are in the ASCII range of UTF-8 and would be fine). std::fstream has two different pointers for reading and writing. Most likely you also have to make sure to open in binary mode.

                                  My claims are fully untested, so I might also be mistaken about the details.

                                  If there's anything in my description that is unfamiliar to you, then just go the way already suggested. There are so many sources of errors with the approach I described that you need a really good reason why you want to do it. And you should like the pain that comes with debugging this if it fails ;-)

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

                                  @SimonSchroeder
                                  The trouble is, changing one character in place is only one of the three possibilities the OP asked about. The other two do not keep the length the same. It is true this particular one could be done by opening ReadWrite, but IMHO encouraging this as the solution is not good/generic. We have had a lot of questions where people think it's fine to update a text file in ReadWrite mode, the sooner they learn the right way to do it for an arbitrary update the better!

                                  Besides, I didn't fancy explaining to the OP about the code needed to save the start-of-line offset, seek to it, update, then seek back to where he was and continue!

                                  1 Reply Last reply
                                  1
                                  • S SimonSchroeder

                                    I'd like to disagree with the rest of you. Certainly, at the proficiency level of the OP he should just write to a new file like suggested. I am also not entirely sure if QFile supports this feature. However, with good old std::fstream (which does both input and output on the same file) it might work. Yes, you cannot insert something into the file. But flipping '#' to '' might actually work. BIG WARNING: This only work with plain old ASCII or Latin-1 (or similar) character encodings. With UTF-8 you have to be careful as hell and should not flip arbitrary characters ('#' and '' are in the ASCII range of UTF-8 and would be fine). std::fstream has two different pointers for reading and writing. Most likely you also have to make sure to open in binary mode.

                                    My claims are fully untested, so I might also be mistaken about the details.

                                    If there's anything in my description that is unfamiliar to you, then just go the way already suggested. There are so many sources of errors with the approach I described that you need a really good reason why you want to do it. And you should like the pain that comes with debugging this if it fails ;-)

                                    Christian EhrlicherC Online
                                    Christian EhrlicherC Online
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #26

                                    @SimonSchroeder said in change/add letter in text file c++:

                                    If there's anything in my description that is unfamiliar to you, then just go the way already suggested. There are so many sources of errors with the approach I described that you need a really good reason why you want to do it. And you should like the pain that comes with debugging this if it fails ;-)

                                    And that's why we did not suggest it - the op is unable to find QIODevice::readLine() by himself so your idea is way out of the possibilities of him.
                                    Flipping an ascii character with QFile is also no problem - with QFile::map() it's a simple search'n'replace

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

                                    1 Reply Last reply
                                    4

                                    • Login

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