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. The Lounge
  3. Inserting text into file

Inserting text into file

Scheduled Pinned Locked Moved The Lounge
3 Posts 3 Posters 0 Views
  • 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.
  • J Offline
    J Offline
    Joshua Nussbaum
    wrote on last edited by
    #1

    I am trying to write data to the middle of file. I use fseek to navigate to the correct position and then use fprintf() to write my buffer to the file. But I find that fprintf() overwrites any data that may be after the current position of the file pointer i.e. I have a text file with the following text: oranges_oranges_oranges_oranges I use this C code to manipulate the file FILE *pFile = fopen("mytext.txt", "r+"); fseek(pFile, 10, SEEK_SET); fprintf("_apples_"); fclose(pFile); The results: oranges_or_apples_anges_oranges My old data was overwritten! Does anyone have a usable method

    B R 2 Replies Last reply
    0
    • J Joshua Nussbaum

      I am trying to write data to the middle of file. I use fseek to navigate to the correct position and then use fprintf() to write my buffer to the file. But I find that fprintf() overwrites any data that may be after the current position of the file pointer i.e. I have a text file with the following text: oranges_oranges_oranges_oranges I use this C code to manipulate the file FILE *pFile = fopen("mytext.txt", "r+"); fseek(pFile, 10, SEEK_SET); fprintf("_apples_"); fclose(pFile); The results: oranges_or_apples_anges_oranges My old data was overwritten! Does anyone have a usable method

      B Offline
      B Offline
      bazar
      wrote on last edited by
      #2

      Read the whole file into a buffer. Do the insertion with buffer manipulation, ie. MoveMemory(), then write the buffer back out. Also use MFC, or even C# to simplify task. Put in oven for 20mins then serve hot

      1 Reply Last reply
      0
      • J Joshua Nussbaum

        I am trying to write data to the middle of file. I use fseek to navigate to the correct position and then use fprintf() to write my buffer to the file. But I find that fprintf() overwrites any data that may be after the current position of the file pointer i.e. I have a text file with the following text: oranges_oranges_oranges_oranges I use this C code to manipulate the file FILE *pFile = fopen("mytext.txt", "r+"); fseek(pFile, 10, SEEK_SET); fprintf("_apples_"); fclose(pFile); The results: oranges_or_apples_anges_oranges My old data was overwritten! Does anyone have a usable method

        R Offline
        R Offline
        Roger Scudder
        wrote on last edited by
        #3

        First I would say that you should understand that the access mode "r+" means read with update... where update means "change". Even though it is, in theory, possible to add data to the middle of a file, I don't know of any way to do it using a language like C. You could possition the file pointer to the insertion point, copy the remainder to a temp holding space such as memory, or a scratch disk file, reposition the file pointer to the insertion offset and overwrite the file with the insert data, then append the data from the temp holding space.

        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

        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups