Results 1 to 5 of 5

Thread: Editing the file using the seek operations

  1. #1
    Join Date
    Jul 2009
    Location
    Bangalore
    Posts
    68
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Editing the file using the seek operations

    Hi All,

    how do i edit the particular line content in a file.txt using seek operations, any code snippet will be useful.
    Thanks & Regards
    Sandeep G.R.

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Editing the file using the seek operations

    I don't think its possible/sensible to directly edit a text file. Because the length of the line will probably have changed, and thus you will overwrite parts of the next line or have some left-over characters... There is no way of inserting data into a file, just overwriting or appending.. So whenever you want to really insert something, you need to rewrite the file entirely.

    That's why text files, that you want to change, should be small enough, that rewriting them entirely, is not a problem.

    Qt Code:
    1. QFile* f = new QFile("test.txt");
    2. if (f->open(QIODevice::ReadOnly | QIODevice::Text))
    3. {
    4. QTextStream* ts = new QTextStream(f);
    5. QStringList lines = ts->readAll().split("\n");
    6. ...
    7. delete ts;
    8. f->close();
    9. }
    10. delete f;
    To copy to clipboard, switch view to plain text mode 

    Writing works the same way, but with WriteOnly, write(lines.join("\n")) .. If you don't need all the lines at once.. you can read and write the text-file line by line using two QFile instances and readLine and write.

    What do you want to do? Update some line separated values? Have a look at QSettings:

    Qt Code:
    1. QSettings settings("/home/petra/misc/myapp.ini", QSettings::IniFormat);
    To copy to clipboard, switch view to plain text mode 

    HIH

    Johannes

  3. #3
    Join Date
    Jul 2009
    Location
    Bangalore
    Posts
    68
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Editing the file using the seek operations

    Hi,

    Thanks for the reply Johannes, Actually 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

    # X Y Z
    5.000, 6.005, 8.340
    45.220, 987.000, 64.000
    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

    Qt Code:
    1. void DatumForm::writefile()
    2. {
    3. QStringList lines;
    4. QString line;
    5. QFile file ( datum.txt );
    6. line.append( xdatumlineEdit->text() );
    7. line.append("," );
    8. line.append( ydatumlineEdit->text() );
    9. line.append("," );
    10. line.append( zdatumlineEdit->text() );
    11. lines += line;
    12. if ( file.open( QIODevice::ReadWrite | QIODevice::Append) )
    13. {
    14. QTextStream stream( &file );
    15. for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it )
    16. stream << *it << "\n";
    17. file.close();
    18. }
    19. }
    20.  
    21. void DatumForm::readfile()
    22. {
    23. QString resulttemp;
    24. QStringList lines;
    25. QString line;
    26. int count = -1;
    27. bool ok;
    28. resulttemp = datumnolineEdit->text();
    29. int linenumber = resulttemp.toInt(&ok, 10);
    30.  
    31. QFile file(datum.txt );
    32. if ( file.open( QIODevice::ReadWrite ) )
    33. {
    34. QTextStream stream( &file );
    35.  
    36. while ( !stream.atEnd() )
    37. {
    38. count += 1;
    39. line = stream.readLine( );
    40.  
    41. if(linenumber == count)
    42. {
    43. resulttemp = line;
    44. datumlines = resulttemp.split(",");
    45.  
    46. Xdatum_Val = datumlines[0].toDouble();
    47. xdatcountlineEdit->setText(QString::number( Xdatum_Val, 'f', 3) );
    48.  
    49.  
    50. Ydatum_Val = datumlines[1].toDouble();
    51. ydatcountlineEdit->setText(QString::number( Ydatum_Val, 'f', 3) );
    52.  
    53.  
    54. Zdatum_Val = datumlines[2].toDouble();
    55. zdatcountlineEdit->setText(QString::number( Zdatum_Val, 'f', 3) );
    56.  
    57.  
    58. }
    59. }
    60. file.close();
    61. }
    62. }
    To copy to clipboard, switch view to plain text mode 

    HOW TO PROCEED WITH THIS? THE USER CAN READ OR WRITE THE FILE.....
    Last edited by grsandeep85; 23rd March 2010 at 04:39.
    Thanks & Regards
    Sandeep G.R.

  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Editing the file using the seek operations

    You didn't quite read my reply, did you? No ReadWrite for TextFiles!!

    What you need to do is read everything, change your line and then rewrite everything.

    Why don't you load the data at program begin, store it in a list for the user to to edit and append, and then write to file, when the user saves/exits?

    Qt Code:
    1. struct XYZTriple {
    2. double x;
    3. double y;
    4. double z;
    5. }
    6.  
    7. // maybe as private member of your class:
    8. class DatumForm : ..
    9. {
    10. private:
    11. QList<XYZTriple> data;
    12. int oldindex;
    13. }
    14.  
    15. void DatumForm::readfile()
    16. {
    17. QFile file("datum.txt");
    18. if (file.open( QIODevice::ReadOnly | QIODevice::QIODevice::Text))
    19. {
    20. QTextStream stream( &file );
    21. while ( !stream.atEnd() )
    22. {
    23. line = stream.readLine( );
    24. datumlines = line.split(",");
    25. if (datumlines.length() == 3) {
    26. XYZTriple datum;
    27. datum.x = datumlines[0].toDouble();
    28. datum.y = datumlines[1].toDouble();
    29. datum.z = datumlines[2].toDouble();
    30. data.append(datum);
    31. } else {
    32. // error in line..
    33. }
    34. }
    35. file.close();
    36. }
    37. }
    38.  
    39. void DatumForm::writefile()
    40. {
    41. QFile file("datum.txt");
    42. if (file.open( QIODevice::WriteOnly | QIODevice::QIODevice::Text | QIODevice::Truncate))
    43. {
    44. QTextStream stream( &file );
    45. for (int i=0;i<data.length();++i)
    46. {
    47. stream << QString("%1,%2,%3\n").arg(data[i].x,0,'f', 3).arg(data[i].y,0,'f', 3).arg(data[i].z,0,'f', 3);
    48. }
    49. stream.flush();
    50. file.close();
    51. }
    52. }
    To copy to clipboard, switch view to plain text mode 
    For your Widgets, don't use simple LineEdits, but QSpinBox and QDoubleSpinBox.

    Now you need to make sure, that your lineNumber is in the right range, and if it is beyond => append data.

    Qt Code:
    1. DatumForm::DatumForm()
    2. {
    3. ..
    4. oldindex = 0;
    5. lineNumberSpinBox->setMinimum(1);
    6. }
    7.  
    8. void DatumForm::lineNumberSpinBoxValueChanged(int i)
    9. {
    10. // save values in array if previous value is valid
    11. if (oldindex > 0)
    12. {
    13. data[oldindex-1].x = xValueDoubleSpinBox->value();
    14. data[oldindex-1].y = yValueDoubleSpinBox->value();
    15. data[oldindex-1].z = zValueDoubleSpinBox->value();
    16. }
    17. // make sure list is big enough:
    18. for (var j=data.length();j<i;++j)
    19. {
    20. // add default values..
    21. XYZTriple datum = {0.0,0.0,0.0};
    22. data.append(datum);
    23. }
    24. // show the values
    25. xValueDoubleSpinBox->setValue(data[i-1].x);
    26. yValueDoubleSpinBox->setValue(data[i-1].y);
    27. zValueDoubleSpinBox->setValue(data[i-1].z);
    28. // save oldindex
    29. oldindex = i;
    30. }
    To copy to clipboard, switch view to plain text mode 
    You could also restrict the lineNumber to the valid ranage with QSpinBox::setRange(1,data.length()) and have an additional append-button.

    Now: Good luck with your project!

    BTW: Code is as is :-> There are probably several typos in there...

    Johannes
    Last edited by JohannesMunk; 23rd March 2010 at 11:02.

  5. #5
    Join Date
    Mar 2010
    Location
    Kawasaki, Japan
    Posts
    13
    Qt products
    Qt/Embedded Qt Jambi
    Platforms
    Symbian S60

    Default Re: Editing the file using the seek operations

    Sandeep,

    My understanding is that files will have sequential data (unless and until you use some specific file formats ex csv etc and have specific drivers to support those - if any).

    The functionality you want is to edit a specific position of the file. In this case you will need to have some place holders which will tell you which data was editted.
    If your data is of fixed length then you can try to overcome the issues mentioned in the post above - by calculating the line eddited and the possible place holder for that data.

    Try for some type of data structure to read your file and let the user edit the the values through some UI - which updates your data structure.
    Then the data structure should be flushed into the file again - once user editing is completed.
    The data structure would have to be a mXn array.
    Your code above can be re-framed into this.

    Regards
    Girish

Similar Threads

  1. editing *.pro file in Visual Studio
    By estel in forum Newbie
    Replies: 2
    Last Post: 8th February 2010, 06:31
  2. Editing a file in Qt4
    By grsandeep85 in forum Qt Programming
    Replies: 3
    Last Post: 22nd December 2009, 09:33
  3. sqlite seek() bug?
    By ibergmark in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2008, 07:44
  4. Graphics operations - Qt 4.1
    By hoborg in forum Newbie
    Replies: 1
    Last Post: 18th February 2006, 14:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.