我正在开发一个运行Linux的嵌入式系统的应用程序.
就我而言,我有一个非常大的文件(与系统的功能相比)作为输入.该文件有一个小标题,其大小只有几百字节.在我的应用程序中,我需要从文件中删除该标头,以便该文件没有标头并仅包含相关数据.通常,我实现如下(伪代码):
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);
Run Code Online (Sandbox Code Playgroud)
这种方法的问题在于它创建了一个临时文件,tmp_file2其大小几乎与输入文件一样大(因为标头非常小).在我的系统中,一切都存储在RAM中,这是非常有限的.创建大型临时文件会导致内存不足错误.
那么如何避免创建一个大的临时文件呢?
提前致谢!