[Someone asked me for clarification on question 19.14 in the comp.lang.c FAQ list. This was my reply.]
Date: Wed, 16 Feb 2000 10:55:50 -0800 (PST)
From: firstname.lastname@example.org (Steve Summit)
Subject: Re: 'q' - answer
In-Reply-To: your message <37D0E174.AB9D0DA0@srasys.co.in>
of Sat, 04 Sep 1999 14:38:04 +0530
> i just want to know whether i can insert a line in a existing file..?
> eventhough the question was very straight forward but not the
> answer( certainly)
> the only word i can understand from your reply is ``YOU PROBABLY CAN'T''
> if you don't mind could u calrify whether it is possible or not.
> actually file appending is allowed in the same way why can't the
> insertion of a line
The problem is that the basic model for file access under virtually all operating systems is ``sequential'', meaning that a file is a stream of bytes which you access sequentially. It's easy to add bytes at the end (that's essentially what you do every time you write to the file), but there's no way to insert bytes in the middle. You can usually seek back into the file and start writing, but you overwrite, rather than inserting. The only way for the OS to allow you to insert bytes would be if it could move all the displaced bytes farther towards the end of the file, but it turns out there's no reasonable way for the OS to allow you to do that, at least not in an efficient or general way. (Some operating systems support record-oriented file types which might allow you to insert records, but in that case you'd be limited to inserting numbers of bytes which were an exact multiple of the record size.)
The answer really is, ``You probably can't.'' If you could, there would be a function call in C's stdio library with a name like ``finsert'', and there would be a system call in the operating system's filesystem interface with a name like ``insert''. But neither of these calls exist, so whether we wish there were a way to insert or not, there simply isn't; the operating system and the C run-time library simply don't support the operation. (At some level, there's only one way to write to a file, and if you're in the middle of the file when you do it, the interpretation is ``overwrite'', not ``insert.'')
> any other procedures is available
You really do have to rewrite the file if you want the effect of inserting text in the middle. This isn't too hard, really (although I agree that sometimes it can be a nuisance).
You may be wondering why you can't insert characters in the middle of a file, when it seems like text editors do it all the time. But text editors never work with a disk file directly; they always work with fancier structures, either in memory or in temporary files, which make inserting and deleting and rearranging text relatively easy. Whenever you edit a file, the text editor reads the file into its internal data structures, then lets you edit, then writes the modified data structures back out to the file (overwriting the whole thing).