Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Inserting text at the beginning of a file in QFile
Qt 6.11 is out! See what's new in the release blog

Inserting text at the beginning of a file in QFile

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 3.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Abdeljalil
    wrote on last edited by
    #1

    Is there any way to append some text to the front of a text file using QFile?
    adding QIODevice::Append flag will always appends to the end, i tried QFile::seek(0) also but did nothing
    any help please?
    (Licenses should be at the top of the source-code files)

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rondog
      wrote on last edited by
      #2

      After opening the file make sure it is not sequential (i.e. file.isSequential() ).

      You mentioned seek(0) didn't work. Maybe (I didn't try this) the value is an offset from where you are now which is the end of the file. If, for example, the file size is 1000 bytes maybe you need to use file.seek(-1000) ?

      I use a shell script and parameters built around the 'sed' command for this. It looks for the old license data (at the top of the file) and updates it as needed. I don't know if something like this is an option but just throwing that out there.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        No, nor is it possible with standard C++. What you can do is create a new file, add your new content and then append the old one. Close both files and replace the original by the new.

        Hope it helps

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved