而不是追加「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