file - Most efficient way to replace a line in a text document? -
i learning code in unix c. far have written code find index of first byte of line want replace. problem sometimes, number of bytes replacing line might greater number of bytes on line. in case, code start overwriting next line. came 2 standard solutions:
a) rather trying edit file in-place, copy entire file memory, edit shifting bytes if necessary , rewriting file.
b) copy line want end-of-file memory , edit.
both suggestions doesn't scale well. , don't want impose restrictions on line size(like every line must 50 bytes or something). there efficient way line replacement ? appreciated.
with text files have "spool" them text delete/insert/replace larger or smaller there.
"spooling" means open temp file in file's directory, reading original file , writing temp file, stop replace/insert/delete starts, thing , copy remainder output. if went fine, unlink original file , rename new file old file.
p.s.: if don't want have restrictions on line size, must use fgetc/fputc
process character-by-character (no sweat; c can pretty fast, disks permitting).
Comments
Post a Comment