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. Howto update text files

Howto update text files

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialcomannouncement
2 Posts 2 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.
  • Y Offline
    Y Offline
    yarp
    wrote on last edited by
    #1

    I'm trying to update a text file by overwritting a part of the text in the middle of the file. I'm using the fwrite function. I manage to overwrite something unfortunately the new text overlaps the text of the contiguous line when it is longer than previous text. For example: My first line Line to override My next line Gives: My first line Overriden line which is longer ext line I wonder if it is possible to insert longer than previous line text with fwrite and if so how to do, if not how to do ;) I don't have the code here but it is something like the folowing (assuming you are reading line 2 only): fFile = fopen("myfile.txt", "r+"); ... fgets(pBuffer)) { ftell(); fWrite("new text"); fflush(fFile) ... } fclose(fFile); Sorry for the code, if you want more I can post it later. Yarp http://www.senosoft.com/

    P 1 Reply Last reply
    0
    • Y yarp

      I'm trying to update a text file by overwritting a part of the text in the middle of the file. I'm using the fwrite function. I manage to overwrite something unfortunately the new text overlaps the text of the contiguous line when it is longer than previous text. For example: My first line Line to override My next line Gives: My first line Overriden line which is longer ext line I wonder if it is possible to insert longer than previous line text with fwrite and if so how to do, if not how to do ;) I don't have the code here but it is something like the folowing (assuming you are reading line 2 only): fFile = fopen("myfile.txt", "r+"); ... fgets(pBuffer)) { ftell(); fWrite("new text"); fflush(fFile) ... } fclose(fFile); Sorry for the code, if you want more I can post it later. Yarp http://www.senosoft.com/

      P Offline
      P Offline
      Prakash Nadar
      wrote on last edited by
      #2

      No, you cant do that way, you have to read the whole file into a buffer, make the change in the buffer and then write to the file. for safetly you write it to a temp file and if the write succeeds then rename it to the original file name ofcource delete the first one. or if you are not too bother about data in the file and would like to take a change, then write it to the same file.


      -prakash

      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