我正在尝试创建一个while
循环,从文件中读取问题并获取用户输入并循环直到它到达问题文件的末尾。
我的问题是循环在要求用户输入之前迭代两次。
fp = fopen("questions.txt","r");
fp2 = fopen("answers.txt","w");
char buff[255];
char input[255];
while (fscanf(fp,"%s",buff)!=EOF)
{
printf(" %s", buff);
printf("answer:");
fgets(input,256,stdin);
fprintf(fp2,"\n");
fprintf(fp2,"%s",buff);
fprintf(fp2,"\n");
fprintf(fp2,"%s",input );
};
答案 0 :(得分:0)
尝试做类似的事情:
char question[SIZE];
char answer[SIZE];
FILE * fin = fopen("questions.txt", "r");
FILE * fout = fopen("answers.txt", "w");
while (fgets(question, SIZE, fin) != NULL) {
printf("%s", question);
fgets(answer, SIZE, stdin);
fputs(answer, fout);
}