使用fscanf读取文件后发生无限循环 [英] Infinite loop after reading a file using fscanf

查看:283
本文介绍了使用fscanf读取文件后发生无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用fscanf读取输入

I am using fscanf to read the input

(2,3)

(3,4)

来自文件,但是每当我运行该程序时,它就会陷入无限循环.输入来自文件abc.txt,该文件必须作为命令行参数传递.代码在下面给出

from a file but it goes into an infinite loop whenever I run the program. The input is from a file say abc.txt which must be passed as a command line argument. The code is give below

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

int main(int argc,char **argv){

    int a,b;
    FILE *fp=fopen(argv[1],"r");

    while(!feof(fp)){
        fscanf(fp,"(%d,%d)",&a,&b);
        printf("\nThe values are :%d %d\n",a,b);
    }
    
    fclose(fp);

}

可能是什么问题?

推荐答案

我认为您需要的只是'\n'

对于您的代码,这对我有用,作为您的示例输入.

This works for me with your code, on a sample input as yours.

 fscanf(fp,"(%d,%d)\n",&a,&b);
                    ^

但是请尝试使用sscanf& fgets进行此类操作.

But try using sscanf & fgets for such operations.

这篇关于使用fscanf读取文件后发生无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆