Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Good way for append some string to every line of exists text file
QtWS25 Last Chance

Good way for append some string to every line of exists text file

Scheduled Pinned Locked Moved C++ Gurus
19 Posts 5 Posters 8.1k 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.
  • V Offline
    V Offline
    vsorokin
    wrote on last edited by
    #1

    I need append various string to every row of text file.

    Example

    Before:

    bq.
    aaa|bbb
    ccc|ddd
    eee|fff

    After:

    bq.
    aaa|bbb|ggg
    ccc|ddd|hhh
    eee|fff|jjj

    Your suggestions?

    --
    Vasiliy

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

      I don't have any other idea than to copy the file line by line (using readLine() of a [[Doc:QTextStream]]) and append the string on the write step.

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

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

        If the files are not too big, I would read it to a QStringList and append there. Then store it again.
        Otherwise, I would use Volker's suggestion

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Robbin
          wrote on last edited by
          #4

          If your file has new line char at the end of the lines, get the whole information (readAll()) and split it using the newline char to get QStringList. From there you can split every QString with your delimiter to get the different values. Volker's suggestion looks more elegant though.

          What's your intention? Are you trying to build some sort of a file like *.csv? If it's intended to be application settings, you will probably be better using QSettings with ini format instead.

          1 Reply Last reply
          0
          • V Offline
            V Offline
            vsorokin
            wrote on last edited by
            #5

            Eus You are right it is CSV :) and sometimes I need add new column.
            Not it's not for settings it's app for observe data, which write data changes to CSV format, for to be able build graph in MS Exel

            --
            Vasiliy

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Robbin
              wrote on last edited by
              #6

              If your app is gonna output the csv on demand, you can always use some databse to save your data in a way which would be easier for you to work with. But I guess that would make it more complex then actually needed.

              Well, the suggestions so far should work for you. I assume you validate the user input for "|" (so you don't get some "extra columns" in your csv :P). QStringList seems the way to go.

              1 Reply Last reply
              0
              • V Offline
                V Offline
                vsorokin
                wrote on last edited by
                #7

                I don't want keep all data from file in memory, it's not needed
                I'm worry about integrity data structure in file.
                So I think I will create temp file for inserting and after insert just change temp file to persistent.

                --
                Vasiliy

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

                  [quote author="Vass" date="1316073685"]I don't want keep all data from file in memory, it's not needed
                  I'm worry about integrity data structure in file.
                  So I think I will create temp file for inserting and after insert just change temp file to persistent.[/quote]

                  That's the way I would go. Maybe add some file locking, so that nobody can change the source file while you are processing it.

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

                  1 Reply Last reply
                  0
                  • AlicemirrorA Offline
                    AlicemirrorA Offline
                    Alicemirror
                    wrote on last edited by
                    #9

                    @Vaas: despite of the number of columns periodically updated, the number of rows in your file is fixed? How many records ?

                    Enrico Miglino (aka Alicemirror)
                    Balearic Dynamics
                    Islas Baleares, Ibiza (Spain)
                    www.balearicdynamics.com

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      vsorokin
                      wrote on last edited by
                      #10

                      [quote author="Alicemirror" date="1316275922"] the number of rows in your file is fixed?[/quote]
                      of course, not.

                      [quote author="Volker" date="1316213165"]
                      That's the way I would go. Maybe add some file locking, so that nobody can change the source file while you are processing it.
                      [/quote]

                      I solved it with temp file, just doing copy of current file, changes it, and rewrite original.
                      I'm not worry about external changes. I'm worry about integrity data structure after immediately power off, for example.

                      --
                      Vasiliy

                      1 Reply Last reply
                      0
                      • AlicemirrorA Offline
                        AlicemirrorA Offline
                        Alicemirror
                        wrote on last edited by
                        #11

                        @Vass: the solution of temp maybe good, I have adopted a similar in a application where I need to download a file and ony when the file is correctly stored on the (tmp folder) device then I can decide if the older file should be delete and substituted by the newly loaded.

                        Despite of this, what I mean is the following:

                        having a set of csv records (rows) where you periodically should add a new value (field -> excel colum) to all the records is it possible that the best approach is to inver rows and columns? It the element growing is the number of columns this will be the number of samples. Obviously it is only an intuituion because I don't know the data nature of your samples.

                        Enrico Miglino (aka Alicemirror)
                        Balearic Dynamics
                        Islas Baleares, Ibiza (Spain)
                        www.balearicdynamics.com

                        1 Reply Last reply
                        0
                        • V Offline
                          V Offline
                          vsorokin
                          wrote on last edited by
                          #12

                          @Alicemirror Unfortunately, rows count not fixed, and changes more often than column count.

                          --
                          Vasiliy

                          1 Reply Last reply
                          0
                          • AlicemirrorA Offline
                            AlicemirrorA Offline
                            Alicemirror
                            wrote on last edited by
                            #13

                            @Vass: you have created a monster! :)

                            Enrico Miglino (aka Alicemirror)
                            Balearic Dynamics
                            Islas Baleares, Ibiza (Spain)
                            www.balearicdynamics.com

                            1 Reply Last reply
                            0
                            • V Offline
                              V Offline
                              vsorokin
                              wrote on last edited by
                              #14

                              :)
                              No, it is just parser of table from web

                              --
                              Vasiliy

                              1 Reply Last reply
                              0
                              • AlicemirrorA Offline
                                AlicemirrorA Offline
                                Alicemirror
                                wrote on last edited by
                                #15

                                Interesting ...
                                I have done somthing time ago and it was really helpful the solutions adopted by the wget command (Linux)

                                Enrico Miglino (aka Alicemirror)
                                Balearic Dynamics
                                Islas Baleares, Ibiza (Spain)
                                www.balearicdynamics.com

                                1 Reply Last reply
                                0
                                • V Offline
                                  V Offline
                                  vsorokin
                                  wrote on last edited by
                                  #16

                                  I mean it's no general parser of tables from web :)
                                  It's one local task on my freelance work. :)

                                  --
                                  Vasiliy

                                  1 Reply Last reply
                                  0
                                  • AlicemirrorA Offline
                                    AlicemirrorA Offline
                                    Alicemirror
                                    wrote on last edited by
                                    #17

                                    Yes I had understood :)

                                    Probably there are not best ways to manage these data. Regarding the proposal of a SQLite database on the device, did you checked if this component has some kind of protection on the transictions to give you the robustness that you need against data loss ?

                                    Enrico Miglino (aka Alicemirror)
                                    Balearic Dynamics
                                    Islas Baleares, Ibiza (Spain)
                                    www.balearicdynamics.com

                                    1 Reply Last reply
                                    0
                                    • V Offline
                                      V Offline
                                      vsorokin
                                      wrote on last edited by
                                      #18

                                      One of customer requrements: nothing databases - only CSV :)

                                      --
                                      Vasiliy

                                      1 Reply Last reply
                                      0
                                      • AlicemirrorA Offline
                                        AlicemirrorA Offline
                                        Alicemirror
                                        wrote on last edited by
                                        #19

                                        Me too I'm fighting with customer in these days. They have always great ideas :) Seems very experts in mobile development...

                                        Enrico Miglino (aka Alicemirror)
                                        Balearic Dynamics
                                        Islas Baleares, Ibiza (Spain)
                                        www.balearicdynamics.com

                                        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