Results 1 to 7 of 7

Thread: Insert something into certain line and column of text file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2019
    Posts
    9
    Qt products
    Qt5
    Platforms
    Windows

    Question Make loop to read line until 11th line.

    I'm newbie and using qt creator. I want to make loop to read until 11th line from beginning of my text file.
    After read read until 11th line of text file, i want to the double spin box value to 11th line.
    My code does not work well. If explain just like use QTextStream, then i cannot understand. I'm newbie.
    Can anyone know? and explain detail to newbie?
    My text file is large, and qt version is 5.13
    Qt Code:
    1. void MainWindow::on_doubleSpinBox_6_editingFinished()
    2. {
    3. QString file("D:\\my text file name");
    4. QFile outputFile(file);
    5. if (outputFile.open(QIODevice::ReadWrite | QIODevice::Text))
    6. {
    7. int i=0;
    8. while (true)
    9. {
    10. if(i==10)
    11. {
    12. QTextStream stream(&outputFile);
    13. file = stream.readLine();
    14. stream << QString::number(ui->doubleSpinBox_6->value());
    15. break;
    16. }
    17. else
    18. {
    19. i=i+1;
    20. }
    21. }
    22.  
    23. }
    24.  
    25. outputFile.close();
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 23rd July 2019 at 08:30. Reason: missing [code] tags

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Make loop to read line until 11th line.

    Quote Originally Posted by hajh90 View Post
    I'm newbie and using qt creator. I want to make loop to read until 11th line from beginning of my text file.
    After read read until 11th line of text file, i want to the double spin box value to 11th line.
    My code does not work well. If explain just like use QTextStream, then i cannot understand. I'm newbie.
    Can anyone know? and explain detail to newbie?
    My text file is large, and qt version is 5.13
    Qt Code:
    1. void MainWindow::on_doubleSpinBox_6_editingFinished()
    2. {
    3. QString file("D:\\my text file name");
    4. QFile outputFile(file);
    5. if (outputFile.open(QIODevice::ReadWrite | QIODevice::Text))
    6. {
    7. int i=0;
    8. while (true)
    9. {
    10. if(i==10)
    11. {
    12. QTextStream stream(&outputFile);
    13. file = stream.readLine();
    14. stream << QString::number(ui->doubleSpinBox_6->value());
    15. break;
    16. }
    17. else
    18. {
    19. i=i+1;
    20. }
    21. }
    22.  
    23. }
    24.  
    25. outputFile.close();
    To copy to clipboard, switch view to plain text mode 
    I think you must first learn the basics - C++. There is no point in using an extensive library when you do not know the basics of the language. What you want to get and the code you write are two different things.

  3. #3
    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: Make loop to read line until 11th line.

    Quote Originally Posted by hajh90 View Post
    My code does not work well. If explain just like use QTextStream, then i cannot understand. I'm newbie.
    Can anyone know? and explain detail to newbie?
    My text file is large, and qt version is 5.13
    I think you are missing some basic understanding that is not really anything to do with Qt.

    The first blind alley you are going down is assuming that you can insert into a text file in-situ. Text files are inherently sequential files with "records" (lines) of varying length. You cannot insert bytes without rewriting every byte that follows. This is why there are two separate files, one being read and the other written, mentioned in my response to your initial post. Files open ReadWrite (as you have tried) will generally be binary data in blocks of known length that are accessed at random, allowing a block to be overwritten without affecting the block before/after.
    Last edited by ChrisW67; 26th July 2019 at 01:32.

Similar Threads

  1. Replies: 2
    Last Post: 14th June 2015, 20:39
  2. Insert data in a text file
    By korwin in forum Newbie
    Replies: 7
    Last Post: 15th July 2011, 02:48
  3. Write one value per line in text file
    By babygal in forum Newbie
    Replies: 11
    Last Post: 1st December 2010, 06:47
  4. Remove a line in a text file
    By jaca in forum Newbie
    Replies: 1
    Last Post: 18th March 2010, 22:13
  5. Replies: 2
    Last Post: 17th November 2006, 11:25

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.