Results 1 to 4 of 4

Thread: How to replace a particular text in a file located at any place..?

  1. #1
    Join Date
    May 2011
    Location
    Mumbai, India
    Posts
    22
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default How to replace a particular text in a file located at any place..?

    hey can any1 tel me how to replace the text in a file which is located at any place......?
    and can can i edit any .ini file....below is my code....

    Qt Code:
    1. QFile file("E:/qtpro/ABC/abc.ini");
    2. file.open(QIODevice::ReadWrite | QIODevice::Text);
    3. file.reset();
    4.  
    5. QTextStream fl(&file);
    6.  
    7. QString str;
    8.  
    9.  
    10. while (!file.atEnd())
    11.  
    12. {
    13.  
    14. str = fl.readLine();
    15.  
    16. if (str == ";extension=abc_123.dll")
    17. {
    18. fl<<"extension=abc_123.dll"<<str<<"\n";
    19. }
    20. //else
    21. //{
    22. //fl<<str<<"\n";
    23. //}
    24. str = file.readLine();
    25.  
    26. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Ketan Shah; 23rd May 2011 at 14:41.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to replace a particular text in a file located at any place..?

    If you want to mange settings in an ini file, check out QSettings - saves you the trouble of manually worrying about reading and writing in to a file - you just set the settings by they name.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    May 2011
    Location
    Mumbai, India
    Posts
    22
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to replace a particular text in a file located at any place..?

    can u tel me, how can i perform the same task for a text file?? is my code correct for it.?

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to replace a particular text in a file located at any place..?

    you need to read the file until where place is where you want to edit it, read the rest of the file, but save the data in memory, seek() back to the place where you want to edit, append your new string, append the rest of the file which you hold in memory, close the file.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Where is the Qbluetooth file located?
    By nachiket bora in forum Qt Programming
    Replies: 6
    Last Post: 11th April 2011, 09:20
  2. Replies: 3
    Last Post: 3rd May 2009, 08:58
  3. Replies: 3
    Last Post: 21st April 2009, 05:25
  4. QString - find and replace text
    By graciano in forum Newbie
    Replies: 3
    Last Post: 24th January 2009, 20:35
  5. Replace text
    By Hz in forum Qt Programming
    Replies: 2
    Last Post: 22nd March 2006, 08:16

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.