How to to overwrite lines in text file in Qt

  • 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.

    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 .
    How can i move to first line and overwrite the data?

    Thank you,

  • @User005

    i am not able to move to first line and it is appending at the last always.

    Firstly, how do you open the log file: if you say " it is appending at the last always" maybe you are opening it for append? Assuming you know that the correct way to move back to the start of the file is via QFileDevice::seek(0) ("rewind").

    Secondly, however, I fear you may be misunderstanding how this would work on a text file. Because lines are (presumably) of different lengths, you cannot simply go back and overwrite and get something sensible out of it. Say your log file has:

    This is line #1
    This is line #2

    If you rewind and send, say, Line #3\n, you will end up with:

    Line #3
    line #1
    This is line #2

    and similar garbage if the line inserted is longer than the one it replaces.

    You can only solve that by truncating the output file when you rewind. Then all will work going forward, but you will have lost all the preceding lines. Otherwise, to 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. Or, don't store it as a text log file, you have to move to something record-based.

  • 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) (QIODevice::ReadWrite | QIODevice::Append|QIODevice::Text);

    if(logFile.size() > MAX_FILE_SIZE)
    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,

  • @User005

    • You have specified QIODevice::Append when opening the file. So it will always append. I already told you you cannot use that if you want to be able to seek & overwrite.

    • You have not stated when you do rewind the file whether you are happy to truncate it so that you lose all the lines that are currently in there and just end up with (the header plus) the new line you now add. When you answer that we will know what the possibilities are. If the answer is "yes, it's OK to truncate" then it can be done. If the answer is (some kind) of "no, I want the other lines somehow remain" then it cannot be done (without copying).

  • i want to retain the other lines and replace one line at a time

  • @JonB Thank you for your response.
    i am new to Qt.
    can you suggest any alternative solution to implement this requirement.
    instead of logging as text file what other approach can we go for to implement this requirement?

    Thank you in advance.

  • @User005
    That depends on what your "requirement" is.

    If you expect it to write the new line into a file and then follow that with all the other lines which were there, you cannot do that without copying the whole file (awful, but maybe acceptable if the file is only 1K long, how frequently/fast do you log lines to the file?). Unless you have fixed length lines (all lines are exactly the same length, truncated or padded if necessary).

    If you can afford to keep the log lines (as a list) in memory, not written to log file, until, say, you exit the program and rewrite the whole file at that point it would work, but not great for a "log" file if you expect to be able to see its updated content as you go along.

    If you were prepared to change it over to, say, a SQLite database then it could be done. But then you won't be able to see the log lines without something which queries them from the database.

    The last paragraph is effectively what something like Windows Event Log is doing if you set it to have a maximum number of entries/size.

    So it depends what your "requirement" is for the external log file and when you expect it to be updated.

  • @JonB Thank you for your reply
    Max file size will be around 10K.
    We are creating external log file and data will be logged everytime there is and error in the system.

    Thank you.

  • @User005
    What else are you expecting me to say other than what I have written above?

  • Thank you for the inputs.

Log in to reply