PDA

View Full Version : How to to overwrite lines in text file in Qt



Anamika s
23rd September 2020, 06:36
Hi All,
i am new to Qt.

i am creating the file in and logging some data to file every time some data is available.
but issue is once i reach max file size defined for file and new data is available then i have to move the first line and overwrite the data in line 1 with new data i am not able to move to first line and it is appending at the last always.

example:
once i reach the max file size and currently the cursor position is at the end of the file then cursor position should be moved to 1st line and new data has to be replace with data in 1st line after replacing the data the cursor should be move to 2nd line in the file.
to achieve this i am using seek() function to move to line but still i am not able to write data it is always


How can i move to first line and overwrite the data?

Thank you,

Regards,
Anamika

Lesiok
23rd September 2020, 08:47
The lines are the same or different length ?
What does it mean "i am not able to write data" ?

Anamika s
23rd September 2020, 09:07
the lines are of different length.

i mean, i am not able to reset the data at the particular line and write new data in that line.

Lesiok
23rd September 2020, 09:32
The file is just a string. Since the lines have different lengths, If you want to change the content of the first line, you have to:
1. Open a new file with a new name.
2. Write the first line to it.
3. Rewrite the remaining lines from the old file.
4. Delete the old file.
5. Rename the new file to the old name.

In my opinion, such a solution to the log makes no sense.

Anamika s
23rd September 2020, 09:45
Thank you for your response.
i am trying to rephrase my question
I am creating the file and logging errors into the file every time when error occurs in the system.
I have defined 1K of file size. So when my file size reaches maximum 1K then i have to start writing the errors (text) from second line of the file (first line will be my header)
It is like as circular way.
I have implemented code as below (Sample code)

QFile logFile (filePath);
QTextStream fileOutput (&logFile);

(void) logFile.open (QIODevice::ReadWrite | QIODevice::Append|QIODevice::Text);

if(logFile.size() > MAX_FILE_SIZE)
{
fileOutput.seek(1);
}
fileOutput << "Data" << endl;
}

sample output:
Sl Id1 Id2 Description Time
1 0 6 unknown description 2020-09-23T13:18:27



is there any way i can achieve this without creating new file and overwriting in same file?

Thank you,

Regards,
Anamika

Lesiok
23rd September 2020, 10:55
1. Only if the lines have a fixed length.
2. fileOutput.seek(1) seeks You to the second byte in file not to the second line.

Anamika s
23rd September 2020, 11:01
Thank you for your response.

remove the old first line and insert a new first line you have to rewrite the entire file, which is a very slow operation and must be repeated for each subsequent line

is there any other approach we can use instead of copying to achieve this ?


Thank you in Advance.

Regards,
Anamika

Lesiok
23rd September 2020, 11:33
no, because you can just add something to the end of the file or replace N characters with other N characters anywhere. You cannot replace 5 old characters with 10 new characters without rewriting the file.
Therefore, I wrote that the principle of operation of this log is wrong.

dorny
23rd September 2020, 14:28
Thanks for the help

d_stranz
23rd September 2020, 20:31
remove the old first line and insert a new first line you have to rewrite the entire file, which is a very slow operation and must be repeated for each subsequent line

is there any other approach we can use instead of copying to achieve this ?


If your file is only 1K in size, then rewriting it will be instantaneous. Your OS will probably have the entire thing cached in memory and will periodically flush it to disk on its own.

Further, if you are only interested in 1K worth of status messages, then you might as well keep it in memory as a QStringList with a fixed size. Each time a new message comes in, you add it to the end and then write the entire thing to disk. Because a QStringList is a list containing strings of any length, once you reach the maximum size, you simply start over by replacing string[0] with the new message, then string[1], etc. until you reach max size, and start over at 0 again.