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.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.
  • R Offline
    R Offline
    RuWex
    wrote on 1 Dec 2022, 10:21 last edited by
    #1

    hi,
    I`m working with text file,
    and I want to read a text file line by line,
    and if line start with # I want to write before it *
    for example I have this file:


    hello
    world
    #house
    tree


    I want it to be:


    hello
    world
    *#house
    tree


    and if it easier I dont mind to do :
    *house
    or instead all the line to do that:


    hello
    world
    *
    tree


    Do you know how to do it?
    thank:)

    C J 2 Replies Last reply 1 Dec 2022, 10:23
    0
    • R RuWex
      1 Dec 2022, 10:21

      hi,
      I`m working with text file,
      and I want to read a text file line by line,
      and if line start with # I want to write before it *
      for example I have this file:


      hello
      world
      #house
      tree


      I want it to be:


      hello
      world
      *#house
      tree


      and if it easier I dont mind to do :
      *house
      or instead all the line to do that:


      hello
      world
      *
      tree


      Do you know how to do it?
      thank:)

      C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 1 Dec 2022, 10:23 last edited by
      #2

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

      Do you know how to do it?

      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.

      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 Dec 2022, 10:30
      4
      • R RuWex
        1 Dec 2022, 10:21

        hi,
        I`m working with text file,
        and I want to read a text file line by line,
        and if line start with # I want to write before it *
        for example I have this file:


        hello
        world
        #house
        tree


        I want it to be:


        hello
        world
        *#house
        tree


        and if it easier I dont mind to do :
        *house
        or instead all the line to do that:


        hello
        world
        *
        tree


        Do you know how to do it?
        thank:)

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 1 Dec 2022, 10:24 last edited by jsulm 12 Jan 2022, 10:24
        #3

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

        Do you know how to do it?

        Yes.
        But you forgot to tell what exactly is not clear.
        To read line from a file: https://doc.qt.io/qt-6/qiodevice.html#readLine-1
        To check whether a string starts with a specific char there is: https://doc.qt.io/qt-6/qstring.html#startsWith-3

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

        R 1 Reply Last reply 1 Dec 2022, 10:30
        3
        • J jsulm
          1 Dec 2022, 10:24

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

          Do you know how to do it?

          Yes.
          But you forgot to tell what exactly is not clear.
          To read line from a file: https://doc.qt.io/qt-6/qiodevice.html#readLine-1
          To check whether a string starts with a specific char there is: https://doc.qt.io/qt-6/qstring.html#startsWith-3

          R Offline
          R Offline
          RuWex
          wrote on 1 Dec 2022, 10:30 last edited by
          #4

          @jsulm
          I know to open ant close file and also to find the #
          I dont know how to change the line

          1 Reply Last reply
          0
          • C Christian Ehrlicher
            1 Dec 2022, 10:23

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

            Do you know how to do it?

            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.

            R Offline
            R Offline
            RuWex
            wrote on 1 Dec 2022, 10:30 last edited by
            #5

            @Christian-Ehrlicher said in change/add letter in text file c++:

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

            Do you know how to do it?

            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.

            this is the only way??
            there are not a way to write to the file?

            C J JonBJ 3 Replies Last reply 1 Dec 2022, 10:34
            0
            • R RuWex
              1 Dec 2022, 10:30

              @Christian-Ehrlicher said in change/add letter in text file c++:

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

              Do you know how to do it?

              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.

              this is the only way??
              there are not a way to write to the file?

              C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 1 Dec 2022, 10:34 last edited by
              #6

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

              there are not a way to write to the file?

              You can also write to a file as we told you.

              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
              1
              • R RuWex
                1 Dec 2022, 10:30

                @Christian-Ehrlicher said in change/add letter in text file c++:

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

                Do you know how to do it?

                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.

                this is the only way??
                there are not a way to write to the file?

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 1 Dec 2022, 10:39 last edited by
                #7

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

                there are not a way to write to the file?

                You can't insert into a file! You can only overwrite what is inside or append at the end of the file. That's why @Christian-Ehrlicher suggested to write the output to a new file and when you're done delete the old file and rename the new so it has the name of the original file.

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

                1 Reply Last reply
                1
                • R RuWex
                  1 Dec 2022, 10:30

                  @Christian-Ehrlicher said in change/add letter in text file c++:

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

                  Do you know how to do it?

                  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.

                  this is the only way??
                  there are not a way to write to the file?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on 1 Dec 2022, 10:39 last edited by JonB 12 Jan 2022, 10:40
                  #8

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

                  there are not a way to write to the file?

                  In a word, you cannot write back to the same file as you are reading from at the same time, it would mess up the data you are reading.

                  R 1 Reply Last reply 1 Dec 2022, 10:44
                  1
                  • JonBJ JonB
                    1 Dec 2022, 10:39

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

                    there are not a way to write to the file?

                    In a word, you cannot write back to the same file as you are reading from at the same time, it would mess up the data you are reading.

                    R Offline
                    R Offline
                    RuWex
                    wrote on 1 Dec 2022, 10:44 last edited by
                    #9

                    @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

                    J JonBJ 2 Replies Last reply 1 Dec 2022, 10:45
                    0
                    • R RuWex
                      1 Dec 2022, 10:44

                      @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

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 1 Dec 2022, 10:45 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 Dec 2022, 10:47
                      1
                      • R Offline
                        R Offline
                        RuWex
                        wrote on 1 Dec 2022, 10:45 last edited by
                        #11

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

                        J 1 Reply Last reply 1 Dec 2022, 10:48
                        0
                        • R RuWex
                          1 Dec 2022, 10:44

                          @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 1 Dec 2022, 10:46 last edited by JonB 12 Jan 2022, 10:47
                          #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
                          • J jsulm
                            1 Dec 2022, 10:45

                            @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 1 Dec 2022, 10:47 last edited by
                            #13

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

                            J JonBJ 2 Replies Last reply 1 Dec 2022, 10:49
                            0
                            • R RuWex
                              1 Dec 2022, 10:45

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

                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 1 Dec 2022, 10:48 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 Dec 2022, 10:49
                              1
                              • R RuWex
                                1 Dec 2022, 10:47

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

                                J Offline
                                J Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on 1 Dec 2022, 10:49 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
                                • J jsulm
                                  1 Dec 2022, 10:48

                                  @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 1 Dec 2022, 10:49 last edited by
                                  #16

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

                                  1 Reply Last reply
                                  0
                                  • R RuWex
                                    1 Dec 2022, 10:47

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

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on 1 Dec 2022, 10:50 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 Dec 2022, 10:53
                                    1
                                    • JonBJ JonB
                                      1 Dec 2022, 10:50

                                      @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 1 Dec 2022, 10:53 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?

                                      J JonBJ 2 Replies Last reply 1 Dec 2022, 10:54
                                      0
                                      • R RuWex
                                        1 Dec 2022, 10:53

                                        @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?

                                        J Offline
                                        J Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on 1 Dec 2022, 10:54 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
                                          1 Dec 2022, 10:53

                                          @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 1 Dec 2022, 10:54 last edited by JonB 12 Jan 2022, 10:57
                                          #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 1 Dec 2022, 11:08
                                          0

                                          1/26

                                          1 Dec 2022, 10:21

                                          • Login

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