I'm trying to learn to alter a line of text within a text file via a console app. So far I can open the file, read each line into a string, identify the text I'd like to alter & alter the string. I can also set the cursor to the position of the begining of the line I want to alter. but when I write to the file it always appends instead of overwriting the line.

This is the text in the file I'm working with:

red Code:
  1. This is a text file for testing.
  2. I plan to use it to develop my fixPath program.
  3. I hope it works.
To copy to clipboard, switch view to plain text mode 

What I'd like to do is remove the word "fixPath from the 2nd line.

Here is the code so far:

Qt Code:
  1. #include <QtCore/QCoreApplication>
  2. #include <QTextStream>
  3. #include <QString>
  4. #include <QFile>
  5.  
  6. QTextStream cout(stdout, QIODevice::WriteOnly);
  7. QTextStream cerr(stderr, QIODevice::WriteOnly);
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11. QCoreApplication a(argc, argv);
  12. QString str1, str2(" fixPath");
  13. QFile file("fixPath.txt");
  14.  
  15.  
  16. if(file.open(QIODevice::ReadWrite))
  17. {
  18. QTextStream fileStream(&file);
  19. while (not fileStream.atEnd())
  20. {
  21. str1 = fileStream.readLine();
  22. if (str1.contains(str2))
  23. {
  24. str1.remove(str2);
  25. fileStream.seek(34);
  26. fileStream.operator<<(str1);
  27. cout << fileStream.pos()<<endl;
  28. }
  29. }
  30. file.close();
  31. }
  32.  
  33. //return a.exec();
  34. }
To copy to clipboard, switch view to plain text mode 

Any help is appreciated.

Ed