View Full Version : Editing a file in Qt4

21st December 2009, 08:30

I have made a application that consists of 4 lineEdits such as number x, y , z the user will input the values to lineEdits and these values should be stored in a file.txt and also the user can change the values and these corresponding values has to be updated in the values the file looks like this

1,, 8.340
2, 45.220, 987.000, 64.000
3, 21.000, 88.980, 73.000

and so on

how to eidt the values if user wants to change and here is the code snippet for accept the values from file then store

void DatumForm::writefile()
QStringList lines;
QString line;
QFile file ( DATUMFILE );
line.append( xdatumlineEdit->text() );
line.append("," );
line.append( ydatumlineEdit->text() );
line.append("," );
line.append( zdatumlineEdit->text() );
lines += line;
if ( file.open( QIODevice::ReadWrite | QIODevice::Append) )
QTextStream stream( &file );
for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it )
stream << *it << "\n";

now what code snippet is to be added to edit or overwrite the file. How to proceed with this.

21st December 2009, 13:29
1. Open file for reading
1. Read file.
2. Populate lineEdits with current file content.
3. Close file.

When user selects "save" then:
1. Open file for writing with QIODevice::Truncate
2. Write lineEdit content to file.
3. Close file.

That is one way.
Another way would be to parse the file and the lineEdits, and write the differences in to the file, but that would be more complected than the above method.

22nd December 2009, 10:18

can you send a code snippet for QIODevice::Truncate..

22nd December 2009, 10:33
What do you mean?
Your own code is a code snippet for it! - see QIODevice::open() documentation.