虽然在C中循环在请求用户输入之前迭代两次

时间:2018-05-07 10:57:50

标签: c while-loop

我正在尝试创建一个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 );
  };

1 个答案:

答案 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);
}