Ответ 1
Вы хотите использовать флаг O_TRUNC
для open()
, используя OR-ing с существующими флагами, указанными выше:
int fout = open(out_file_name, O_WRONLY | O_CREAT | O_TRUNC, 644);
Это приведет к усечению файла. Ниже приведена информация на странице руководства для открытия (2).
O_TRUNC
If the file already exists and is a regular file and the open
mode allows writing (i.e., is O_RDWR or O_WRONLY) it will be
truncated to length 0. If the file is a FIFO or terminal device
file, the O_TRUNC flag is ignored. Otherwise the effect of
O_TRUNC is unspecified.