GETC(FP)造成麻烦 [英] getc(fp) causing trouble

查看:245
本文介绍了GETC(FP)造成麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code。

#include<stdlib.h>
#include<stdio.h>
int main(int argc,char** argv)
{
    char a;
    a=9;
    FILE * fp;
    fp=fopen(argv[1],"r");
    while(a!= EOF)
    {
        a=fgetc(fp);
        printf("\n%d",a);
    }
}

输出到这是正常的,但最后我得到一个奇怪的字符与-1(因为我打印的整数值。

The output to this is alright but at the end I am getting a weird character with -1 (since I am printing integer value.

如何才能停止它在 EOF
此外,这是什么字?

How to stop it at EOF only? Also what is this character?

推荐答案

除了在其他答案的方法,你也可以这样做:

Besides the methods in the other answers, you can also do like this:

while ((a = fgetc(fp)) != EOF)
{
    printf("%d\n", a);
}

现在你有一些替代解决方案。 :)

Now you have a few alternative solutions. :)

编辑:作为R ..所以好心提醒我们,你还必须更改 A 的类型INT

As R.. so kindly reminds us, you also have to change the type of a to int.

这篇关于GETC(FP)造成麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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