Results 1 to 3 of 3

Thread: Problem: Reading and editing text file data

  1. #1
    Join Date
    Mar 2011
    Location
    New Delhi, India
    Posts
    31
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Post Problem: Reading and editing text file data

    I have a text file having data something like:

    ww1
    ww2
    ww3
    ww4
    I want to search for a particular string in this file, so i am reading this file. When the word is found say "ww3" , i wish to edit that entry to something like $$ww3 or ( add anything). I tried using pos() and seek(), but it's deleting all text data from file leaving one or two chars.

    I would like to know some way to do this. Please help.

    Thanks in advance.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem: Reading and editing text file data

    Rewrite the entire file:

    (Psuedocode, you would use QFile for this)
    Qt Code:
    1. while (!eof(file))
    2. {
    3. str = readlinefromfile();
    4. if (str == "ww3") writelinetofile("$$ww3"); else writelinetofile(str);
    5. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to squidge for this useful post:

    dipeshtech (2nd May 2011)

  4. #3
    Join Date
    Mar 2011
    Location
    New Delhi, India
    Posts
    31
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Problem: Reading and editing text file data

    Thanks squidge! But, i am using ReadWrite mode in QFile, which is not rewriting the whole file rather it is writing the desired content at the end of file. Something like this...

    Input file:

    bb1
    bb2
    bb3


    o/p getting:

    bb1
    bb2
    bb3
    bb1
    $$bb2
    bb3

    desired o/p:
    only:

    bb1
    $$bb2
    bb3


    Qt Code:
    1. QFile file_bl1("E:\\SMS\\dSenderBlackList.txt");
    2. file_bl1.open(QIODevice::ReadWrite | QIODevice::Text);
    3. file_bl1.reset();
    4. QTextStream bl_in1(&file_bl1);
    5. QString bl_line1;
    6. bl_line1 = bl_in1.readLine();
    7. while(!bl_line1.isNull())
    8. {
    9.  
    10. if(str==bl_line1)
    11. {
    12. bl_in1<<"$$"<<str<<"\n";
    13. }
    14. else
    15. {
    16. bl_in1<<bl_line1<<"\n";
    17.  
    18. }
    19. bl_line1 = bl_in1.readLine();
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 

    I am trying , but it would be good if someone can help me in figuring out how to perform aforementioned. Thanks.


    Added after 29 minutes:


    Thanks Squidge!! PROBLEM is solved. Just took a different pointer for writing.
    Last edited by dipeshtech; 2nd May 2011 at 23:47.

Similar Threads

  1. Reading from text file
    By jerkymotion in forum Qt Programming
    Replies: 5
    Last Post: 17th March 2011, 11:26
  2. Replies: 1
    Last Post: 27th January 2011, 09:19
  3. Editing text file via console app
    By cejohnsonsr in forum Newbie
    Replies: 1
    Last Post: 30th August 2010, 21:39
  4. Reading data from xls file
    By addu in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2010, 09:33
  5. reading data from file
    By offline in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2010, 10:31

Tags for this Thread

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.