我需要在文件中的每行末尾添加一個文本。我有以下代碼:在一個文件純行代碼中的行尾添加文本
FILE *tmp_copy = tmpfile();
file = fopen (argv[2], "rw");
if ((file != NULL)) {
char line [ 128 ]; /* or other suitable maximum line size */
while((fgets (line, sizeof line, file) != NULL)) {
fputs(line, tmp_copy);
}
fclose (file);
rewind(tmp);
char *p;
/* Reopen file now with write permissions */
fopen(argv[2], "w");
while ((p = fgets(line, 1024, tmp))!=NULL) {
//line[strlen(line)-1] = '\0'; /* Clear away newline */
//sprintf(line, "%s %s\n", line, more);
strcpy(line,"true");
//fputs(line, file);
}
fclose(file);
fclose(tmp);
}
}
我編輯了我的代碼。仍然不能正常工作 但它不工作。爲什麼?
_這是不是「不工作」? –
http://forums.devshed.com/showpost.php?p=136468&postcount=5 –