I've recently posted a thread called file modification and the problem has been solved........now could some1 kindly explain why is it necessary to delete and rename the file to insert or delete a character into the file at a particular location


6 Years
Discussion Span
Last Post by griswolf

@masijade explained it in the first answer on your [thread=342627]other thread[/thread]. Let me try it again:
For most OSs, you can't guarantee that the contents of a file can be held entirely in memory, so if you want to modify a file, you have to do it in 'reasonable sized' chunks. Since you are reading the file in chunks, you better not write over the top of it while that is going on. Instead, you open a reader on the original file, and a writer on a temporary file, then you

  read in a chunk from the reader; 
  modify the chunk; 
  write out the modified chunk to the temporary file;
  if something goes wrong: Try to delete the temp file, and die
  back to top of loop
  UNTIL original file is all modified
Everything went well (or we died above): remove the original file (just to be sure)
rename the temp file to the original name

Edited by griswolf: n/a


ya....i understood that......wht i wanted to know is that is it common to all languages.....and OSs or in OSs like unix will there be any difference


and by modification i meant both inserting and deleting.....sorry if im pestering with too many doubts :)


This behavior is common to all OSs (nearly). The language is irrelevant to this issue. Everything from assembly language on up.

It is a result of the need to handle files which can be very large: An individual file can be 100s or thousands of megabytes in size, whereas the amount of available memory is likely to be a few megabytes. It is true for inserts, deletes, re-ordering and any other modification on the original file.

There are some non-standard OSs that don't clearly distinguish files from memory, and for those OSs, things are handled differently. You are very unlikely to ever run into such an OS (I've only heard about them).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.