📄 insertf.20000216.html
字号:
<html><!-- Mirrored from c-faq.com/osdep/insertf.20000216.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:02:31 GMT --><head><title></title></head><body><p>[Someone asked me for clarificationon question 19.14 in the comp.lang.c FAQ list.This was my reply.]<p>Date: Wed, 16 Feb 2000 10:55:50 -0800 (PST)<br>Message-Id: <200002161855.KAA11838@mail.eskimo.com><br>From: scs@eskimo.com (Steve Summit)<br>Subject: Re: 'q' - answer<br>In-Reply-To: your message <37D0E174.AB9D0DA0@srasys.co.in><br> of Sat, 04 Sep 1999 14:38:04 +0530<br>Cc: scs@eskimo.com<p>You wrote:<br>> i just want to know whether i can insert a line in a existing file..?<br>> eventhough the question was very straight forward but not the<br>> answer( certainly)<br>> yes/no<br>> the only word i can understand from your reply is ``YOU PROBABLY CAN'T''<p>Right.<p>> if you don't mind could u calrify whether it is possible or not.<br>> actually file appending is allowed in the same way why can't the<br>> insertion of a line<p>The problem is that the basic model for file access undervirtually all operating systems is ``sequential'', meaning thata file is a stream of bytes which you access sequentially.It's easy to add bytes at the end (that's essentially what youdo every time you write to the file), but there's no way toinsert bytes in the middle. You can usually seek back into thefile and start writing, but you overwrite, rather than inserting.The only way for the OS to allow you to insert bytes would be ifit could move all the displaced bytes farther towards the end ofthe file, but it turns out there's no reasonable way for the OSto allow you to do that, at least not in an efficient or generalway. (Some operating systems support record-oriented file typeswhich might allow you to insert records, but in that case you'dbe limited to inserting numbers of bytes which were an exactmultiple of the record size.)<p>The answer really is, ``You probably can't.'' If you could,there would be a function call in C's stdio library with aname like ``<TT>finsert</TT>'', and there would be a system call in theoperating system's filesystem interface with a name like``<TT>insert</TT>''. But neither of these calls exist, so whether wewish there were a way to insert or not, there simply isn't;the operating system and the C run-time library simply don'tsupport the operation. (At some level, there's only one way towrite to a file, and if you're in the middle of the file whenyou do it, the interpretation is ``overwrite'', not ``insert.'')<p>> any other procedures is available<p>You really do have to rewrite the file if you want the effectof inserting text in the middle. This isn't too hard, really(although I agree that sometimes it can be a nuisance).<p>You may be wondering why you can't insert characters in themiddle of a file, when it seems like text editors do it all thetime. But text editors never work with a disk file directly;they always work with fancier structures, either in memory orin temporary files, which make inserting and deleting andrearranging 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 backout to the file (overwriting the whole thing).</body><!-- Mirrored from c-faq.com/osdep/insertf.20000216.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:02:31 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -