how to remove n bytes in a file?

D

Dasn

Hi.
Suppose we have a very large file, and wanna remove 'n' bytes in the
middle of the file. My thought is:
1, read() until we reach the bytes should be removed, and mark the
position as 'pos'.
2, seek(tell() + n) bytes
3, read() until we reach the end of the file, into a variable, say 'a'
4, seek(pos) back to 'pos'
5, write(a)
6, truncate()

If the file is really large, the performance may be a problem.
Is there a clever way to finish? Could mmap() help? Thx
 
G

Grant Edwards

Hi.
Suppose we have a very large file, and wanna remove 'n' bytes in the
middle of the file. My thought is:
1, read() until we reach the bytes should be removed, and mark the
position as 'pos'.
2, seek(tell() + n) bytes
3, read() until we reach the end of the file, into a variable, say 'a'
4, seek(pos) back to 'pos'
5, write(a)
6, truncate()

If the file is really large, the performance may be a problem.
Is there a clever way to finish? Could mmap() help? Thx

Instead of reading till the end of the file in step 3, do it a
chunk at a time in a loop.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top