Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to add a lineat the beginnig of a file?

How to add a lineat the beginnig of a file?

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
6 Posts 5 Posters 0 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.
  • L Offline
    L Offline
    lucia4444es
    wrote on last edited by
    #1

    :confused:I need to insert a new line at the beginning of a text file but i don´t know how to do this without overwriting the first line. This is my code: ... file.SeekToBegin(); file.WriteString(text); ... I've also tried this: ... file.SeekToBegin(); file.WriteString("\r\n"); file.WriteString(text); ... but it doesn´t work. Could anyone help me? Thanks Lucia

    C L 2 Replies Last reply
    0
    • L lucia4444es

      :confused:I need to insert a new line at the beginning of a text file but i don´t know how to do this without overwriting the first line. This is my code: ... file.SeekToBegin(); file.WriteString(text); ... I've also tried this: ... file.SeekToBegin(); file.WriteString("\r\n"); file.WriteString(text); ... but it doesn´t work. Could anyone help me? Thanks Lucia

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Do you have a valid file handle, do you have write-access to this file? Check the return codes.

      1 Reply Last reply
      0
      • L lucia4444es

        :confused:I need to insert a new line at the beginning of a text file but i don´t know how to do this without overwriting the first line. This is my code: ... file.SeekToBegin(); file.WriteString(text); ... I've also tried this: ... file.SeekToBegin(); file.WriteString("\r\n"); file.WriteString(text); ... but it doesn´t work. Could anyone help me? Thanks Lucia

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        It is not possible to 'insert' new data at the begining of the file. A solution for doing that is using a temporary file. In this file, you first copy the first line you need to insert, then copy all the data from the original file. When this is done, you take this data and copy it into the original file (erasing all it's previous contents).

        Y L 2 Replies Last reply
        0
        • C Cedric Moonen

          It is not possible to 'insert' new data at the begining of the file. A solution for doing that is using a temporary file. In this file, you first copy the first line you need to insert, then copy all the data from the original file. When this is done, you take this data and copy it into the original file (erasing all it's previous contents).

          Y Offline
          Y Offline
          ytod
          wrote on last edited by
          #4

          I think allocating a buffer that's long enough to store previous data could also work.

          1 Reply Last reply
          0
          • C Cedric Moonen

            It is not possible to 'insert' new data at the begining of the file. A solution for doing that is using a temporary file. In this file, you first copy the first line you need to insert, then copy all the data from the original file. When this is done, you take this data and copy it into the original file (erasing all it's previous contents).

            L Offline
            L Offline
            lucia4444es
            wrote on last edited by
            #5

            Thanks for the reply. I have already thought of this solution but i searched the simplest one. Ok, i'll try using a temporary file

            D 1 Reply Last reply
            0
            • L lucia4444es

              Thanks for the reply. I have already thought of this solution but i searched the simplest one. Ok, i'll try using a temporary file

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Be sure you have write access to the folder in which the temporary file will be created. If the file is not gigantic (e.g., 100MB or more), you could just read it into a heap-based buffer, write the new text to the file and then append the contents of the buffer. That's about as simple as it gets.


              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups