C 文件I/O未追加
我正在尝试使用文件I/O制作一个程序,但是我遇到了一个问题,文件没有附加。我没有任何错误,一切正常,但文件没有附加C 文件I/O未追加,c,C,我正在尝试使用文件I/O制作一个程序,但是我遇到了一个问题,文件没有附加。我没有任何错误,一切正常,但文件没有附加 fp = fopen("file.txt", "a"); rewind(fp); if(fp == NULL){ fprintf(stderr, "File cant be opened\n"); return 0; }else{ getString(value); printf("File appended!"); fclos
fp = fopen("file.txt", "a");
rewind(fp);
if(fp == NULL){
fprintf(stderr, "File cant be opened\n");
return 0;
}else{
getString(value);
printf("File appended!");
fclose(fp);
}
如果要附加到文件,则需要在其中写入。在你的例子中,你不是
FILE *file = NULL;
file = fopen(fileName, "a"); // "a" for "append"
fprintf(file, "This string is appended to the file");
fprintf(file, "This string is appended to the file"); // You can call it multiple times
fclose(file);
就你而言:
fp = fopen("file.txt", "a");
if(fp == NULL){
fprintf(stderr, "File cant be opened\n");
return 0;
} else {
fprintf(fp, "%s\n", value);
fclose(fp);
}
附加到文件的代码在哪里?如果要追加,为什么要倒带?是否要向文件中写入任何内容?getString(value)是一个调用方法以添加到列表中的方法,假设我输入hello,然后hello被添加到列表中,我假设它也会追加到文件中?如果我是C的新手,请纠正我的错误。因此,在本例中,我需要执行fprintf(fp,“%s”,value)?@DontStopLearn如果要追加,则需要包含追加代码,而不仅仅是调用文件。在这种情况下,fprinf将起作用。