简体   繁体   English

从命令提示符执行程序时,fgets出现问题

[英]problems occurring with fgets when a program is executed from command prompt

scanf("%d",&a);

fflush(stdin);

fgets(ch,SIZE,stdin);    //SIZE=100 and ch is a char array

i have just used only this portion of code.It works fine normally.But when i am executing this programm from cmnd prmpt by using file.exe < input.txt > output.txt it's giving a garbage value in output.txt file. 我只使用了这部分代码。它正常工作。但是当我使用file.exe < input.txt > output.txt从cmnd prmpt执行此程序时,它在output.txt文件中给出了垃圾值。

You can flush stdin using getchar or fgetc(stdin) : 您可以使用getcharfgetc(stdin)刷新stdin

static void flush_stdin(void)
{
    int c;

    while ((c = fgetc(stdin)) != '\n' && c != EOF);
}

scanf("%d",&a);
flush_stdin();
fgets(ch,SIZE,stdin);    //SIZE=100 and ch is a char array

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

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