PDA

View Full Version : Reading data from cvs file into String & deleting part of String data n write 2 file



Channareddy
7th June 2011, 11:09
Hi,
I AM NEW TO Qt.My question is how do i read data from cvs file which contains information of many users created by ADMIN in one column.Now if i want to delete one user then i have to remove his details from the file.So please help in achieving this.
Presently i am able to read data from file into QString but i am not getting how to manipulate the data inside the String after comparing it with user name and id(these are used to find & delete the user from file).Please share the code if any which may be helpful for me...

Thanks in advance..:):mad:

joyer83
7th June 2011, 11:48
Umm, I think you should read the data from the file into a string list (QStringList) where each item is a user.
Then you modify the list, like removing of adding of new items/users.
And finally, you write the list back to the file.

Hardest part is the reading of the file into that list. Rest should be a breeze to implement.

ChrisW67
7th June 2011, 23:22
A naïve approach would be:

Open input file and output file: QFile::open()
Read a line from the input file: QFile::readLine() or QTextStream::readLine()
Split the line on the comma character: QString::split()
Check the fields and if the line is to be kept write it to the output file: QFile::write() or QTextStream operator <<
Repeat from 2 until input file exhausted
Close files
On successful completion replace input file with output file


However, "CSV File" covers a multitude of slightly different formats: quotes, no quotes, embedded commas, embedded new lines etc. More complex handling is required for many of these scenarios.

Channareddy
8th June 2011, 07:36
Thank you so much...:) It helped me a lot..
Finally I achieved it..:)

Thank you Chris once again..:) I was able to do it..
Meet u again with some problems...:)