简体   繁体   English

fscanf 不返回 EOF 或 fscanf 在 C 中进入无限循环

[英]fscanf not returning EOF or fscanf going to infinite loop in C

I am trying to write a few lines to a file.我正在尝试将几行写入文件。 After the line are written, when I try to read those lines from file using fscanf it is going into infinite loop.写完行后,当我尝试使用fscanf从文件中读取这些行时,它会进入无限循环。 fprintf is working but fscanf is going to an infinite loop. fprintf正在工作,但fscanf将进入无限循环。

#include<stdio.h>
#include<stdlib.h>

  void main()
       {
        FILE *fp;
        int roll;
        char name[25];
        float marks;
        char ch;
        fp = fopen("file.txt","w");           
        if(fp == NULL)
        {
            printf("\nCan't open file or file doesn't exist.");
            exit(0);
        }

        do
        {
             printf("\nEnter Roll : ");
             scanf("%d",&roll);

             printf("\nEnter Name : ");
             scanf("%s",name);
             printf("\nEnter Marks : ");
             scanf("%f",&marks);

             fprintf(fp,"%d%s%f",roll,name,marks);

             printf("\nDo you want to add another data (y/n) : ");
             ch = getche();

             }while(ch=='y' || ch=='Y');

            printf("\nData written successfully...");
              
              
              
            printf("\nData in file...\n");

            while((fscanf(fp,"%d%s%f",&roll,name,&marks))!=EOF)
            printf("\n%d\t%s\t%f",roll,name,marks);
                
              

            fclose(fp);
       }

You have opened the file for writing (mode "w"), so your scanf calls are almost certainly failing.您已打开文件进行写入(模式“w”),因此您的scanf调用几乎肯定会失败。 Even if you fix the mode, it is not at all surprising that:即使您修复了模式,也不奇怪:

while((fscanf(fp,"%d%s%f",&roll,name,&marks))!=EOF)

goes into an infinite loop.进入无限循环。 If the next character in the stream is not a valid character in an integer, then scanf will return zero and not consume it.如果 stream 中的下一个字符不是 integer 中的有效字符,则scanf将返回零并且不使用它。 It will repeatedly attempt to read that character as an integer and repeatedly fail.它将反复尝试将该字符读取为 integer 并反复失败。 The correct approach here is probably to stop using scanf entirely, but a quick work-around may be something like:此处正确的方法可能是完全停止使用scanf ,但快速解决方法可能类似于:

int rv;
while( (rv = fscanf(fp,"%d%s%f",&roll,name,&marks)) != EOF ){
    if( rv == 3 ){
        printf(...);
    } else {
        /* probably the right thing to do is break out of
           the loop and emit an error message, but maybe 
           you just want to consume one character to progress
           in the stream. */
        if( fgetc(fp) == EOF ){
            break;
        }
    }
}

it would be more common to write while( 3 == fscanf(...)) and just emit an error message on bad input, but something like the above kludge might be useful (depending on your use case).编写while( 3 == fscanf(...))并在错误输入时发出错误消息会更常见,但类似上述 kludge 的内容可能有用(取决于您的用例)。

But you need to fix the open mode.但是您需要修复打开模式。 Probably you just want to close the file after the write loop (you certainly need to flush it before you can expect to read from the file) and re-open in with mode "r".可能您只想在写循环之后关闭文件(您当然需要先刷新它才能期望从文件中读取)并以模式“r”重新打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM