简体   繁体   中英

how to delete only specified part of a file in c?

I have written my own cp function which copies only half of the mp4 file from one partition to my own additionally created partition. This additionally created partition is half of the size of mp4 file and i am playing mp4 file from this partition. After this half mp4 file is played, i want to remove the blocks of this part of a file so that I can copy the rest of the file to this partition and play it. I am using low level i/o functions open,read,write,close in C file handling to write cp function.

This functionality is going to be dependant on the OS you are using. I am not aware of a universal way to do this. On windows, you can use the function _chsize() in unix based systems you want ftruncate() . you can use each of these functions to set the size of the file to 0, effectively getting rid of your allocated blocks.

I would use mmap(2) for that purpose and get rid of the scratch partition. On most systems, this is way faster since disk I/O is handled in the fashion that is just like swap file, which is very highly optimized and accerlerated.

When start playing, map the first portion of the file to a memory address and start working from that. Then when the end of the current part is coming map the next part in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM