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::on_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 =
Re: Insert something into certain line and column of text file
Quote:
Originally Posted by
hajh90
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.
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
Code:
void MainWindow::on_doubleSpinBox_6_editingFinished()
{
QString file("D:\\my text file name");
{
int i=0;
while (true)
{
if(i==10)
{
file = stream.readLine();
stream <<
QString::number(ui
->doubleSpinBox_6
->value
());
break;
}
else
{
i=i+1;
}
}
}
outputFile.close();
Re: Insert something into certain line and column of text file
Quote:
Originally Posted by
ChrisW67
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.
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,
_
Re: Make loop to read line until 11th line.
Quote:
Originally Posted by
hajh90
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
Code:
void MainWindow::on_doubleSpinBox_6_editingFinished()
{
QString file("D:\\my text file name");
{
int i=0;
while (true)
{
if(i==10)
{
file = stream.readLine();
stream <<
QString::number(ui
->doubleSpinBox_6
->value
());
break;
}
else
{
i=i+1;
}
}
}
outputFile.close();
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.
Re: Make loop to read line until 11th line.
Quote:
Originally Posted by
hajh90
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.