PDA

View Full Version : Insert something into certain line and column of text file



hajh90
22nd July 2019, 01:48
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 =

ChrisW67
22nd July 2019, 05:17
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() (https://doc.qt.io/qt-5/qstring.html#insert) and QString::number() (https://doc.qt.io/qt-5/qstring.html#number-6)
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() (https://doc.qt.io/qt-5/qfile.html#copy-1)


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.

hajh90
22nd July 2019, 22:25
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


void MainWindow::on_doubleSpinBox_6_editingFinished()
{
QString file("D:\\my text file name");
QFile outputFile(file);
if (outputFile.open(QIODevice::ReadWrite | QIODevice::Text))
{
int i=0;
while (true)
{
if(i==10)
{
QTextStream stream(&outputFile);
file = stream.readLine();
stream << QString::number(ui->doubleSpinBox_6->value());
break;
}
else
{
i=i+1;
}
}

}

outputFile.close();

hajh90
22nd July 2019, 22:26
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() (https://doc.qt.io/qt-5/qstring.html#insert) and QString::number() (https://doc.qt.io/qt-5/qstring.html#number-6)
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() (https://doc.qt.io/qt-5/qfile.html#copy-1)


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.

anda_skoa
23rd July 2019, 08:42
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,
_

Lesiok
23rd July 2019, 08:52
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


void MainWindow::on_doubleSpinBox_6_editingFinished()
{
QString file("D:\\my text file name");
QFile outputFile(file);
if (outputFile.open(QIODevice::ReadWrite | QIODevice::Text))
{
int i=0;
while (true)
{
if(i==10)
{
QTextStream stream(&outputFile);
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.

ChrisW67
26th July 2019, 01:09
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.