而不是追加“ABC”(因此最终得到一个文件的完整ABCABCABC的....),无论多少次,我跑这一点,只MYFILE containst“ABC” ... 。我该如何追加?用C失败追加,直接覆盖
#include <stdio.h>
#include <string.h>
int main(){
char strng[10];
strcpy(strng,"abc");
FILE *my_file;
my_file = fopen("myfile","a+");
if (my_file == NULL){ printf("problem\n");}
fwrite(strng, sizeof(strng), 1, my_file);
printf("appending %s\n",strng);
fclose(my_file);
}
顺便说一句,不相关的问题:不要使用的strcpy()。如果你的系统有,使用strlcpy()。如果你在Windows上考虑StringCchCopy()。作为最后的手段,使用strncpy(),但请仔细阅读其注意事项。 – asveikau 2009-12-05 20:23:39
根据http://www.opengroup.org/onlinepubs/009695399/functions/fwrite.html,我使用了参数顺序。什么是正确的顺序? 返回值是1(包括如果我执行命令frwite不止一次在节目中),但只有一个“ABC”还是会被写入到文件....:/ – 2009-12-05 20:27:33
可能我建议fprintf中(my_file, “%s”,strng);代替fwrite。 – SiegeX 2009-12-05 20:30:03