I'm developing an application for embedded system running Linux.
In my case, I have a quite big file (compared to the capability of the system) as input. The file has a small header whose size is just a few hundred bytes. In my application, I need to remove that header from the file so the file will have no header and contain only relevant data. Normally, I'd implement like following (pseudo code):
char *input_file = "big_input.bin";
char *tmp_file1 = "header.bin";
char *tmp_file2 = "data.bin";
/* Copy the content of header from input file to tmp_file1 */
_copy_header(tmp_file1, input_file);
/* Copy the data from input file to tmp_file2 */
_copy_data(tmp_file2, input_file);
/* Rename temp file to input file */
unlink(input_file);
rename(tmp_file2, input_file);
The problem with this approach is that it creates a temporary file tmp_file2
whose size is almost as big as input file (because the header is very small). In my system, everything is stored on RAM, which is very limited. Creating a big temporary file causes an out-of-memory error.
So how can I avoid creating a big temporary file?