Results 1 to 12 of 12

Thread: replace line of a file

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

    Default replace line of a file

    Hi
    I am newbie on Qt.

    I have a file which contains these lines
    Qt Code:
    1. First line
    2. Second line
    3. Third line
    To copy to clipboard, switch view to plain text mode 

    Is there any way to replace first line with a new line but no change in other lines,which should result in

    Qt Code:
    1. This is line one
    2. Second line
    3. Third line
    To copy to clipboard, switch view to plain text mode 

    I am using qlineedit for write data in file.

    can anyone please give code snippet.

    Advance thanx.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: replace line of a file

    Nothing to do with Qt per se. General steps:
    • Open original file for reading
    • Open temporary file for writing
    • Write lines from original file until you reach the line you want to replace
    • Write the replacement line
    • Skip the line from the original file
    • Write lines from original file until end of file
    • Close temporary and original file
    • If all of above successful, remove original file and copy/rename temporary file.


    Start with QFile, QTextStream and QTemporaryFile in the Qt docs. The temporary file could be in-memory; see QBuffer.

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

    frankiefrank (28th August 2013)

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

    Default Re: replace line of a file

    many many thanx ChrisW67 for your repl.

    But if you dont mind can you please give the sample code/full code.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: replace line of a file

    No. Try it yourself.

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

    Default Re: replace line of a file

    hmm.......

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: replace line of a file

    The thing is, this is not the QtCentre Write Your Program For You Service. People here are willing to help those with evidence of having tried to do it themselves. I have already given you a program flow and told you all the necessary components. To do any more would be me writing the program for you... IMHO you will not learn as much from the exercise.

  8. The following user says thank you to ChrisW67 for this useful post:

    masuk (1st February 2011)

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

    Default Re: replace line of a file

    I need the solution code

    thanks

  10. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: replace line of a file

    ...and I people who try at least. And by the way from where else do you copy and past your application? What have you tried so far? What problems are you facing? Had you have a look at QFile, QTextStream and other Qt classes?

    If you can't do it, hire someone you can and pay him money or start to learn to program.

  11. #9
    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.

  12. #10
    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....

  13. #11
    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..........

  14. #12
    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.