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. Writing to the middle of a file

Writing to the middle of a file

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 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.
  • I Offline
    I Offline
    Inatha
    wrote on last edited by
    #1

    I am trying to open a file using fopen for append and switching its current position using fseek to modify its current contents.

    oFile=fopen(strcat(f\_SELF,"1.exe"), "a+b");
    fseek(oFile,f\_POS,SEEK\_SET);
        putw(0x00000000,oFile);
    //fwrite((void\*)VALUE,4,1,oFile);
    //putw((int)VALUE,oFile);
    fclose(oFile);
    

    When I use fwrite on the file it returns a C0000005 exception and with the current code the new file info is appended to the bottom of the file instead of overwritten in the middle. How do I write to were my pointer is set?

    A 1 Reply Last reply
    0
    • I Inatha

      I am trying to open a file using fopen for append and switching its current position using fseek to modify its current contents.

      oFile=fopen(strcat(f\_SELF,"1.exe"), "a+b");
      fseek(oFile,f\_POS,SEEK\_SET);
          putw(0x00000000,oFile);
      //fwrite((void\*)VALUE,4,1,oFile);
      //putw((int)VALUE,oFile);
      fclose(oFile);
      

      When I use fwrite on the file it returns a C0000005 exception and with the current code the new file info is appended to the bottom of the file instead of overwritten in the middle. How do I write to were my pointer is set?

      A Offline
      A Offline
      anu_88
      wrote on last edited by
      #2

      From MSDN:

      When a file is opened with the "a" or "a+" access type, all write
      operations occur at the end of the file. The file pointer can be
      repositioned using fseek or rewind but is always moved back to the end of
      the file before any write operation is carried out. Thus, existing data
      cannot be overwritten.

      When the "r+", "w+", or "a+" access type is specified, both reading and
      writing are allowed (the file is said to be open for “update”). However,
      when you switch between reading and writing, there must be an intervening
      fflush, fsetpos, fseek, or rewind operation. The current position can be
      specified for the fsetpos or fseek operation, if desired.

      So instead of using append mode try to use r+ it is for both reading and writing. Hope this helps. -- modified at 0:31 Monday 18th December, 2006

      I 1 Reply Last reply
      0
      • A anu_88

        From MSDN:

        When a file is opened with the "a" or "a+" access type, all write
        operations occur at the end of the file. The file pointer can be
        repositioned using fseek or rewind but is always moved back to the end of
        the file before any write operation is carried out. Thus, existing data
        cannot be overwritten.

        When the "r+", "w+", or "a+" access type is specified, both reading and
        writing are allowed (the file is said to be open for “update”). However,
        when you switch between reading and writing, there must be an intervening
        fflush, fsetpos, fseek, or rewind operation. The current position can be
        specified for the fsetpos or fseek operation, if desired.

        So instead of using append mode try to use r+ it is for both reading and writing. Hope this helps. -- modified at 0:31 Monday 18th December, 2006

        I Offline
        I Offline
        Inatha
        wrote on last edited by
        #3

        Using w+ overwrites the previous file, so I get an x size file because the data between 0x00 to where where fseek is set is filled with 0's and whatever was in fputw is at the end. I need to reserve the previous contents too.

        A 1 Reply Last reply
        0
        • I Inatha

          Using w+ overwrites the previous file, so I get an x size file because the data between 0x00 to where where fseek is set is filled with 0's and whatever was in fputw is at the end. I need to reserve the previous contents too.

          A Offline
          A Offline
          anu_88
          wrote on last edited by
          #4

          "r+"

          Opens for both reading and writing. (The file must exist.)

          "w+"

          Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.

          If your file already exists,try using r+

          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