[英]Does fwrite() overwrite when called multiple times?
Example: I am going through a tree and fwriting based on preorder traversal.示例:我正在遍历一棵树并根据前序遍历进行写作。
This is pseudocode:这是伪代码:
void func x ('','')
{
do something
helper (char * filename, thing to be written);
}
void helper (char * filename, thing to be written)
{
FILE * fp = fopen (filename,"w");
fwrite(, , ,fp);
fclose(fp);
return 0;
}
Would it overwrite previous entries?它会覆盖以前的条目吗? If so, how can I prevent this?如果是这样,我该如何防止这种情况发生? Is there a better way to do this?有一个更好的方法吗?
As kaylum mentioned, the short answer is yes it does.正如 kaylum 所提到的,简短的回答是肯定的。
fopen with "w" will open the file at the beginning, so any writes will truncate and overwrite. fopen with "w" 将在开头打开文件,因此任何写入都将被截断并覆盖。 In order to avoid this, either reuse the file pointer returned by fopen every time, or use "a" in order to set fwrite to append mode.为了避免这种情况,要么重复使用 fopen 每次返回的文件指针,要么使用“a”将 fwrite 设置为 append 模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.