Results 1 to 12 of 12

Thread: replace line of a file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2011
    Location
    Gordion
    Posts
    52
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: replace line of a file

    Qt Code:
    1. QFile file(iplogini);
    2. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    3. { //you can send a message or throw error signal here
    4. return;
    5. }
    6.  
    7. QTextStream in(&file);
    8. while (!in.atEnd())
    9. {
    10. line.append(in.readLine());
    11. }
    12. QString lineedit =line.at(4);
    13. lineedit.replace("StorageFolder=","Storage Folder Path"); //first arg is text that, which will
    14. //gone a be change and second arg is text will replace the other one...
    To copy to clipboard, switch view to plain text mode 
    First open file, if file exist
    second read file, line by line, and write each line to a string list element
    third if your text file will not be change (my file is a static ini file) and if you now that which line you want to replace, we can get it from qstringlist elements with "at()" public function

    so if i can get the line, which i wanted, i can write in another string, after that with replace function i can replace char to in.

    Sorry for bad english!

    hope this help
    Last edited by kosasker; 3rd February 2011 at 13:15.

  2. #2
    Join Date
    Jan 2011
    Posts
    39
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: replace line of a file

    many many thanks kosasker....

  3. #3
    Join Date
    Jan 2011
    Posts
    39
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: replace line of a file

    thanks to all....specially thanks ChrisW67 and kosasker..........

  4. #4
    Join Date
    Jan 2011
    Posts
    39
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: replace line of a file

    solution code:
    Qt Code:
    1. void Form::replace()
    2. {
    3. QFile file("E:/workplace/file1.txt");
    4. file.open(QIODevice::ReadOnly | QIODevice::Text);
    5. QTextStream in(&file);
    6. QString line = in.readLine();
    7. QString line1 = in.readLine()+"\n";
    8. QString line2 = in.readLine()+"\n";
    9. file.close();
    10.  
    11.  
    12. QFile ofile("E:/workplace/file2.txt");
    13. ofile.open(QIODevice::ReadWrite | QIODevice::Text);
    14. QTextStream out(&ofile);
    15. out << ui->lineEdit_2->text()+"\n";
    16. out << line1;
    17. out << line2;
    18. ofile.close();
    19.  
    20. file.remove();
    21. ofile.rename("E:/workplace/file2.txt","E:/workplace/file1.txt");
    22.  
    23. this->close();
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. New line when writing a File
    By locke in forum Qt Programming
    Replies: 5
    Last Post: 17th May 2011, 11:27
  2. How to write something on a text file on a certain line?
    By Bong.Da.City in forum Qt Programming
    Replies: 1
    Last Post: 17th December 2010, 21:01
  3. Replies: 3
    Last Post: 3rd May 2009, 08:58
  4. How to write something in line of txt file
    By Zergi in forum Qt Programming
    Replies: 2
    Last Post: 24th December 2007, 10:02
  5. How to read line from file
    By Krishnacins in forum Newbie
    Replies: 10
    Last Post: 1st June 2006, 23:14

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
  •  
Qt is a trademark of The Qt Company.