question about fwrite function.....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • system55
    New Member
    • Aug 2006
    • 25

    question about fwrite function.....

    what does fwrite do when it is used? does it append to the file, overwrites an existing data or just insert the data to the wxisting file??
    because i would like to use it to overwrite a data and also to insert...
    i'm just wondering what fwrite does...
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    fwrite writes data to the file at the current insertion point. Whether this is overwriting data or appending data to the file is entirely dependent on the location of the insertion point and this is dependent on the way the file was opened and/or if the insertion point has been moved (using fseek for instance).

    There are no functions that actually insert data into the middle of an existing file, to do this you need to read the data at the end of the file, write your new data and then re-write the end of the file.

    Comment

    • system55
      New Member
      • Aug 2006
      • 25

      #3
      thank you for your answer...
      what if the location of the insertion pointer is at the middle of the file, if i use fwrite to insert some bytes, will it be overwritten?
      does the use of "r+" when the file is opened mean that i can also write on that file?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        If the insertion pointer is at the middle of the file and you write to the file then you will over write the data that is all ready in the file.

        "r+" does allow you to write to the file as well as read from it.

        Comment

        • system55
          New Member
          • Aug 2006
          • 25

          #5
          thank you again...
          what should i do in order to insert, for example 10 bytes, at a certain position in the file???

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            I already said
            1. Set the file insertion point to the position that you wish to insert data in the file.
            2. Copy the file contents from this point to the end of a file into memory (or a temporary file).
            3. If required reset the file insertion point to the position that you wish to insert data
            4. Write the data to be inserted
            5. Write all the stored 'end of file' data after the new data

            Comment

            • system55
              New Member
              • Aug 2006
              • 25

              #7
              thanks for the advoce.........

              Comment

              Working...