mmap( ) vs read( )

前端 未结 4 2067
说谎
说谎 2020-12-28 18:50

I\'m writing a bulk ID3 tag editor in C. ID3 tags are usually at the beginning of an mp3 encoded file, although older (version 1) tags are at the end. The app is designed

4条回答
  •  死守一世寂寞
    2020-12-28 19:24

    I don't know if standard POSIX functions reside inside what you are allowed or you will to use for the development but think about these two functions:

    int ftruncate(int fildes, off_t length);
    int truncate(const char *path, off_t length);
    

    defined in unistd.h, which can be used to truncate a file up to a specified length. In this way you could easily

    • find where ID3 tags frame begins (don't know if you can compute it easily by just reading the header of the MP3 file but I guess yes)
    • save the offset
    • close the file
    • truncate the file with the provided function
    • open the file in append binary mode and write new tags

    I'm not sure about the performance, you should test this method, but it should load much less things inside ram while providing a senseful way of doing it.

提交回复
热议问题