September 15, 2011

Vass Vass
Hobby Entomologist
738 posts

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

Page  
1

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

Example

Before:

aaa|bbb
ccc|ddd
eee|fff

After:

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

Your suggestions?

 Signature 


Vasiliy

18 replies

September 15, 2011

Volker Volker
Robot Herder
5428 posts

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

September 15, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

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

 Signature 

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

September 15, 2011

Eus Eus
Lab Rat
121 posts

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.

September 15, 2011

Vass Vass
Hobby Entomologist
738 posts

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

 Signature 


Vasiliy

September 15, 2011

Eus Eus
Lab Rat
121 posts

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.

September 15, 2011

Vass Vass
Hobby Entomologist
738 posts

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.

 Signature 


Vasiliy

September 17, 2011

Volker Volker
Robot Herder
5428 posts
Vass wrote:
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.

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.

September 17, 2011

Alicemirror Alicemirror
Lab Rat
825 posts

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

 Signature 

Enrico Miglino (aka Alicemirror)
Tech Consulting
Islas Baleares, Ibiza (Spain)
http://www.contesti.eu

September 18, 2011

Vass Vass
Hobby Entomologist
738 posts

Alicemirror wrote:
the number of rows in your file is fixed?

of course, not.

Volker wrote:
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.

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.

 Signature 


Vasiliy

September 18, 2011

Alicemirror Alicemirror
Lab Rat
825 posts

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

 Signature 

Enrico Miglino (aka Alicemirror)
Tech Consulting
Islas Baleares, Ibiza (Spain)
http://www.contesti.eu

September 18, 2011

Vass Vass
Hobby Entomologist
738 posts

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

 Signature 


Vasiliy

September 18, 2011

Alicemirror Alicemirror
Lab Rat
825 posts

@Vass: you have created a monster! :)

 Signature 

Enrico Miglino (aka Alicemirror)
Tech Consulting
Islas Baleares, Ibiza (Spain)
http://www.contesti.eu

September 18, 2011

Vass Vass
Hobby Entomologist
738 posts

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

 Signature 


Vasiliy

September 18, 2011

Alicemirror Alicemirror
Lab Rat
825 posts

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

 Signature 

Enrico Miglino (aka Alicemirror)
Tech Consulting
Islas Baleares, Ibiza (Spain)
http://www.contesti.eu

September 18, 2011

Vass Vass
Hobby Entomologist
738 posts

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

 Signature 


Vasiliy

Page  
1

  ‹‹ [Solved] [Moved] Can QList<T*> take subclasses of T?      [moved]calling c application in Qt ››

You must log in to post a reply. Not a member yet? Register here!