Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

Delete a Line from a file in C Language

I want to delete certain lines in a file and insert certain lines in the same file based on whether certain parts of the line match a specified string. Is there a way of doing this without using a temporary file to copy the contents to and so on?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Problem is, a file is (essentially) an array of bytes on disk (or whatever other physical substrate, but, bytes anyway!), and "a line" can take a varying number of bytes; so to insert or remove lines (unless you're always rigorously replacing a line with another line of exactly the same length in bytes) would require "shifting" all the rest of the file "up" or "down" by the difference in bytes... which can be an extremely onerous operation (as the rest of the file can be gigabytes even if you're just changing one line's length by 1 byte towards the beginning of the file).

So, such operations can be incredibly onerous and therefore are typically never offered as primitives in ANY language supporting files with variable line length (C, Python, Java, C++, Ruby, or ANY other such language). It's extremely unlikely that you really need to pay such a potentially unbound cost in performance AND risk (a system or disk crash during the "shift" of GB or data up or down can destroy the usabilty of your whole, huge file), when the perfectly simple, adequate, fast, safe, and reasonable technique you're trying to avoid has basically ZERO downsides (so it's far from obvious WHY are you trying to avoid it?).

Use a result-file different from the source-file, when done mv the result-file over the source-file (an atomic operation on most systems if you're within the same filesystem), and you really do have the best of all possible worlds.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...