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
417 views
in Technique[技术] by (71.8m points)

c - Inserting text into file at specific offset using Win32 API

I am looking for a way to make speedy modifications to large multi-gigabyte files. Do the Win32 API support the ability to insert text into a file at a specific offset without having to rewrite the entire file back to disk from the very beginning or from the offset of the change?

Consider an example. Let's say we have the text "test" repeated over and over in a file that is 1 GB in size. If I want to go to the 500 MB offset and insert the text "new", is there a way to insert it without having to rewrite the entire file from the beginning, and/or without having to rewrite the last 500 MBs of it?

Can it be done using Win32 API? If not, are there any strategies to optimize a text insertion operation like this to maximize speed?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

There are methods to rewrite only the portion after the insertion point, but generally, no - to insert something at a particular point in a file, you must re-write everything after that point.

This boils down to the way files are stored on disk - typically in chunks, such that this operation is either not possible or not easy. For 99% of the cases, this doesn't matter, so the API doesn't expose a way of doing this.

If you have control over the file format, you can engineer ways such that you can write data to the end of the file, but have some tracking data to say "this stuff really belongs here".


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

...