Results 1 to 10 of 10

Thread: How to to overwrite lines in text file in Qt

  1. #1
    Join Date
    Sep 2020
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

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

    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
    Last edited by Anamika s; 23rd September 2020 at 11:13.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,521
    Thanked 278 Times in 273 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to to overwrite lines in text file in Qt

    The lines are the same or different length ?
    What does it mean "i am not able to write data" ?

  3. #3
    Join Date
    Sep 2020
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How to to overwrite lines in text file in Qt

    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.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,521
    Thanked 278 Times in 273 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to to overwrite lines in text file in Qt

    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.

  5. #5
    Join Date
    Sep 2020
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How to to overwrite lines in text file in Qt

    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
    Last edited by Anamika s; 23rd September 2020 at 11:19.

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,521
    Thanked 278 Times in 273 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to to overwrite lines in text file in Qt

    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.

  7. #7
    Join Date
    Sep 2020
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How to to overwrite lines in text file in Qt

    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

  8. #8
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,521
    Thanked 278 Times in 273 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to to overwrite lines in text file in Qt

    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.

  9. #9
    Join Date
    Sep 2020
    Posts
    1
    Qt products
    Platforms
    MacOS X

    Default Re: How to to overwrite lines in text file in Qt

    Thanks for the help

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,693
    Thanks
    259
    Thanked 752 Times in 742 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to to overwrite lines in text file in Qt

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. How to know how many lines a text files has?
    By Momergil in forum Qt Programming
    Replies: 2
    Last Post: 15th July 2013, 05:45
  2. overwrite pdf file
    By terhje in forum Newbie
    Replies: 2
    Last Post: 22nd February 2012, 13:41
  3. Replies: 6
    Last Post: 17th April 2009, 11:09
  4. Replies: 2
    Last Post: 28th March 2009, 11:11
  5. I want to get lines of text
    By newplayer in forum Qt Programming
    Replies: 11
    Last Post: 29th July 2008, 10:03

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.