这是我第一次来这里,所以对任何需要道歉的事情表示歉意。
我有一个程序,可以将文本保存到单独的文件中,并在执行完成后保存。我的问题是,由于 fprintf(我们被教导使用的函数)的性质,每次我重新运行程序时,我输入到该文件中的任何内容都会被删除和替换。
有人能解决这个问题吗?
提前致谢。
到目前为止我的代码是:
#include <stdio.h>
#include <string.h>
int main (void) {
FILE *Mem_r;
FILE *Mem_w;
char word[257];
char comp = 'n';
int count, ret = 1;
Mem_w = fopen("Memory_w.txt", "w");
Mem_r = fopen("Memory_r.c", "r");
if (Mem_w != NULL && Mem_r != NULL) {
printf("What do you want me to remember?\n");
scanf("%s", &word);
fprintf(Mem_w, "%s ", &word);
}
else {
printf("Error");
}
return(0);
}
最佳答案
您可以像这样更改代码。
Mem_w = fopen("Memory_w.txt", "a");
use the
a
option instead ofw
, as thew
will instruct the stream to discard the file to be opened. Just as @Peter commented.
关于c - 在执行之间保存信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44213666/