Results 1 to 2 of 2

Thread: Editing text file via console app

  1. #1
    Join Date
    Aug 2010
    Location
    Joplin, Mo
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Editing text file via console app

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Editing text file via console app

    You need to truncate the file, rewind it and overwrite all of it. For files as we know them there is no way to cut out data from the middle of a file.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Editing in text box of web page : QWebView
    By girishgowda in forum Qt Programming
    Replies: 0
    Last Post: 13th July 2010, 11:42
  2. Editing the file using the seek operations
    By grsandeep85 in forum Qt Programming
    Replies: 4
    Last Post: 24th March 2010, 04:30
  3. Editing a file in Qt4
    By grsandeep85 in forum Qt Programming
    Replies: 3
    Last Post: 22nd December 2009, 09:33
  4. QTableWidgetItem Text Editing
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2009, 02:23
  5. Console text color
    By skyphyr in forum Qt Programming
    Replies: 4
    Last Post: 11th December 2006, 17:19

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.