PDA

View Full Version : How to write data into a file



grsandeep85
20th July 2009, 10:17
Hi All,

I have a input data file which contains some text as the following, which is inputed using 4 lineedits n X Y Z then i need to write these into a text file when i press the enter button from the key board and the dialog closes then again i need to open the dialog to enter the next values and continues.


#DATA 4 (n X Y Z)
1 123.000 -123.000.123.000
2 456.000 78.000 -12345.000
.........
.........
.........

and so on. But if i try to write the values into the file it is overwriting on the same place i.e., current values is writing into the file but the previous value is erased. i have attached my code and please help us.

Lykurg
20th July 2009, 10:20
Use QIODevice::Append when opening the file. please only paste the relevant code directly in the post using the [ CODE ] tags.


EDIT: And instead of creating a temporary QStringList, write the values direct in the stream.

nish
20th July 2009, 10:21
are you opening the file using QIODevice::Append ?

EDIT : Too late....brother got too fast this time..

grsandeep85
20th July 2009, 11:32
Thanks Sir its working fine..

And also i need to read the data which is written into the file by giving the serial number n(say 2) in lineedit n the corresponding values of X Y & Z( 456.000, 78.000, -12345.000 ) should be read and display the values in the X Y & Z lineedits. Please help us it would be a great pleasure.

Lykurg
20th July 2009, 12:14
Either store the content of your file inside a QHash etc, and modify that and only when exiting the application save to file.

Or perform full search on the file every time you a certain set of data.

Or better use a database for that. SQLite is good for that since it is build in and a self-contained, serverless, zero-configuration, transactional SQL database engine.

grsandeep85
20th July 2009, 13:48
Hi Sir,

I need to do it in lineedits only and not in SQLite, please help me how to proceed further.

Lykurg
20th July 2009, 14:06
I need to do it in lineedits only and not in SQLite, please help me how to proceed further.

The one has nothing to do with the other! (one is displaying and editing, the other storing)


You can store your data in a custom file, which you read/write each time you need it.
Store your data in a QHash and read/write the content to a custom file only on start and exit your application.
Like the above, only use XML with a QDomDocument
For storing the data use SQLite (which behaves like a custom file...)

grsandeep85
21st July 2009, 08:00
I am using qt-3.3.7 but QHash is not there in this version.

nish
21st July 2009, 08:02
i think there is something in 3.3 like QDict

Lykurg
21st July 2009, 08:51
I am using qt-3.3.7 but QHash is not there in this version.
If you can, update your version... Otherwise QHash was just an example, you also can use a simple QStringList or QMap, a own structure etc...