Hi ALL,
I'm trying to create a file and update some data in the file.

e.g Data is like

Helllo
Hi
Qt
Test

Below is my code. but last line is overwriting. Ineed new text to be insert a new line and append the string. Plz tell me how to correct this code snipset.

Qt Code:
  1. #include <QtCore/QCoreApplication>
  2. #include <iostream>
  3. #include <qfile.h>
  4. #include <qtextstream.h>
  5. using namespace std;
  6.  
  7.  
  8. void File(QString myFilename, QString myData)
  9. {
  10. QFile file(myFilename);
  11. if ( file.open( QIODevice::ReadWrite) )
  12. {
  13. QTextStream stream( &file );
  14.  
  15.  
  16. // while (stream.atEnd())
  17. // {
  18. // stream << QChar((int)'\n');
  19. //
  20. // }
  21. stream <<myData << endl;
  22.  
  23. }
  24. else
  25. {
  26. qDebug( "Could not create file %s" );
  27. }
  28. }
  29.  
  30. int main(int argc, char *argv[])
  31. {
  32. QCoreApplication a(argc, argv);
  33.  
  34. QString file("D:\\test.txt");
  35.  
  36. File(file, "1St Line: Hello");
  37. File(file, "2Nd Line : Hi");
  38. File(file, "3rd Line : Qt");
  39. File(file, "4th Line : Test");
  40. return a.exec();
  41. }
To copy to clipboard, switch view to plain text mode 

Thank u all,
Phillip