Hello everybody,
I am facing an issue:
I want to be able to insert some text at the end of every line of a text file.
I tried many things with QTextStream on a QFile but i am still not able to do the trick.
Initialization of a test file.
I store in the endLinePos the position of the endline character.
in_file
->open
(QFile::ReadWrite |
QFile::Truncate);
qDebug() << "INITFILE";
for(int i=0;i<10;i++)
{
qDebug() << "INITFILE - line - "<<i;
in << "123456789\n";
endLinePos[i]=(i*10+9);
}
qDebug() << "END INITFILE";
in_file->close();
in_file->open(QFile::ReadWrite | QFile::Truncate);
qDebug() << "INITFILE";
QTextStream in(in_file);
for(int i=0;i<10;i++)
{
qDebug() << "INITFILE - line - "<<i;
in << "123456789\n";
endLinePos[i]=(i*10+9);
}
qDebug() << "END INITFILE";
in_file->close();
To copy to clipboard, switch view to plain text mode
in_file
->open
(QFile::ReadWrite);
for(int lineNum=0; lineNum<10; lineNum++)
{
in.seek(endLinePos[lineNum]);
in<<"toto";
}
in_file->open(QFile::ReadWrite);
QTextStream in(in_file);
for(int lineNum=0; lineNum<10; lineNum++)
{
in.seek(endLinePos[lineNum]);
in<<"toto";
}
To copy to clipboard, switch view to plain text mode
The output:
123456789toto456789toto456789toto456789toto456789toto456789toto456789toto456789toto456789toto456789toto
123456789toto456789toto456789toto456789toto456789toto456789toto456789toto456789toto456789toto456789toto
To copy to clipboard, switch view to plain text mode
I can see that my data "toto" has been placed at the right position but it replaced the original content.
My aim would be to generate an output like
123456789toto
123456789toto
123456789toto
...
123456789toto
123456789toto
123456789toto
...
To copy to clipboard, switch view to plain text mode
Loading the full file in the memory is not a solution: The real data are far too big (each line : 130000 char in a file having around 500 lines).
Thanks for your advices!
Korwin
Bookmarks