如何使用scanf函数使用whil​​e循环scanf函数后!= EOF [英] How to use scanf after using while loop scanf !=EOF

查看:158
本文介绍了如何使用scanf函数使用whil​​e循环scanf函数后!= EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来输入。在文件到我的program.gcc ./\"file< file.in结果
因为有多个输入我必须使用While循环EOF,但
因为我不能再scanf函数。
这是我计划的模拟版本。谢谢

 的#include<&stdio.h中GT;诠释主(){
    INT ARR [100];
    INT NUM;
    诠释计数= 0;
    而(scanf函数(%d个,&安培;!ARR [计)= EOF){
        算上++;
    }
    的printf(%d个\\ N,编曲[0]);
    scanf函数(%D,试验#);
    的printf(%d个\\ N,NUM);
}


解决方案

试试这个

 则freopen(CON,R,标准输入); //这对于Windows。 为/ dev / tty的为* nix中?
scanf函数(%D,试验#);

I have to input a .in file into my program.gcc ./"file" < file.in
Because there are more than one input I have to use the While loop EOF, but because of that I can't scanf again. Here's a mock version of my program. Thank you

#include <stdio.h>

int main() {
    int arr[100];
    int num;
    int count = 0;
    while( scanf("%d", &arr[count]) != EOF ) {
        count++;
    }
    printf("%d\n", arr[0]);
    scanf("%d", &num);
    printf("%d\n", num);
}

解决方案

try this

freopen("con:", "r", stdin);//this for windows. "/dev/tty" for *nix ?
scanf("%d", &num);

这篇关于如何使用scanf函数使用whil​​e循环scanf函数后!= EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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