ftruncate on file opened with fopen

前端 未结 3 1490
无人及你
无人及你 2021-01-20 20:14

Platform is Ubuntu Linux on ARM. I want to write a string to a file, but I want every time to truncate the file and then write the string, i.e. no append.

I have thi

3条回答
  •  温柔的废话
    2021-01-20 20:44

    If you want to literally "truncate the file then write", then it's sufficient to:

    f=fopen("/home/user1/refresh.txt","w");
    fputs("some string",f);
    fclose(f);
    

    Opening the file in the mode w will truncate it (as opposed to mode a which is for appending to the end).

    Also calling fclose will flush the output buffer so no data gets lost.

提交回复
热议问题