What ‘s the best way to add a header (e.g. file metadata) to an existing file if the file size is around 1-2GB?
in C++
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The best way is to simply write the header to a new file, then append the contents of the old.
C++ doesn’t provide a way to insert at the start of an existing file so that’s really the best bet. Just make sure you read large enough chunks from the old file and append them to the new one. While buffering will alleviate most of the problems of doing this in small chunks, you’ll still have the performance degradation of more function calls.
This may be minimal but it’s still there. For a 2G file, I’d probably start by doing it in half-gig chunks unless memory is at a premium. This allows for larger file sizes without too much memory wastage and four read/write calls is unlikely to be a performance issue.
But, as with all optimisations, measure, don’t guess. There are various low level things which can affect performance that the C++ standards document makes no mention of (and rightly so). Since your question makes no mention of a specific operating system, I’ve answered based on that, but those specific operating systems may both (a) react differently; and (b) provide other non-standard calls which can be made faster.