Results 1 to 7 of 7

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

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

    Question Insert something into certain line and column of text file

    I'm using qt creator and i want to insert double spin box value into 11th line 19th column of my text file. I don't know how to set up the line and column.
    Can someone help me??

    void MainWindow:n_doubleSpinBox_6_editingFinished()
    {
    QFile file("D:\\filename");
    if (file.open(QIODevice::ReadWrite | QIODevice::Text))
    {
    QTextStream in(&file);
    QString text;
    text = in.readLine();
    in << QString::number(ui->doubleSpinBox_6->value());
    }
    file.close();

    This is my text file

    parameter PW_x = "i want to insert the value here"
    parameter PW_y =
    parameter PD =
    parameter PC =

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

    Default Re: Insert something into certain line and column of text file

    Quote Originally Posted by hajh90 View Post
    I'm using qt creator and i want to insert double spin box value into 11th line 19th column of my text file. I don't know how to set up the line and column.
    Can someone help me??
    Something like:
    • Open file you wish to insert into. QTextStream
    • Open a temporary file to hold the result. QTextStream and QTemporaryFile
    • Loop 10 times reading a line from the input file and writing it to the output.
    • Read the 11th line from the input
    • Insert the value into the line at character 19. QString::insert() and QString::number()
    • Write the modified line to the output
    • Loop over remaining lines in the input file, writing each to the output file
    • Close both files
    • If there have been no errors copy the temporary file over the original file. QFile::copy()


    If the files are small then this can be done by reading the entire file into a QStringList, manipulating it in memory, then writing the result. There is more than one way to do this.

  3. #3
    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

  4. #4
    Join Date
    Jul 2019
    Posts
    9
    Qt products
    Qt5
    Platforms
    Windows

    Question Re: Insert something into certain line and column of text file

    Quote Originally Posted by ChrisW67 View Post
    Something like:
    • Open file you wish to insert into. QTextStream
    • Open a temporary file to hold the result. QTextStream and QTemporaryFile
    • Loop 10 times reading a line from the input file and writing it to the output.
    • Read the 11th line from the input
    • Insert the value into the line at character 19. QString::insert() and QString::number()
    • Write the modified line to the output
    • Loop over remaining lines in the input file, writing each to the output file
    • Close both files
    • If there have been no errors copy the temporary file over the original file. QFile::copy()


    If the files are small then this can be done by reading the entire file into a QStringList, manipulating it in memory, then writing the result. There is more than one way to do this.
    Thanks a lot. Can you explain more detail how to use these command to make code? I am newbie.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,396
    Thanks
    37
    Thanked 1,542 Times in 1,493 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Insert something into certain line and column of text file

    ChrisW67 already explained that in quite some detail.

    Apparently you only read the first step, then ignored the second, only read the first three words from the third, did steps 4-6 and ignored most of the rest.

    Cheers,
    _

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,454
    Thanked 271 Times in 266 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.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,543
    Thanks
    13
    Thanked 1,577 Times in 1,506 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.