与fgets()函数不能接受命令提示信息 [英] fgets() function can't accept message from command prompt

查看:266
本文介绍了与fgets()函数不能接受命令提示信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使这是提示用户从控制台键入消息,然后显示,消息用户键入,一个C程序如下面的例子:

  C:> promptTest
键入消息>>>>测试
类型:测试C:>

这是我的code:

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
诠释主(){
    味精的char [32];
    的printf(请键入消息>>>> \\ t的);
    与fgets(味精,sizeof的(MSG),标准输入);
    味精[strlen的(MSG) - 1] ='\\ 0';
    的printf(输入:%S \\ n,味精);
    返回0;
}

它可以建立在两个Windows7的和CentOS并且它可以正常在Windows像上面运行。

但是,它不能在CentOS运行。我的意思是什么都不接受来自提示如下图所示的任何消息:

  $ ./promptTest
键入消息>>>>测试类型:
$

我该如何解决这个问题?

下面是关于我的机器的信息。

  $猫的/ etc / redhat的释放
CentOS版本6.4(决赛)
$
$拱
x86_64的
$


解决方案

我找到了解决办法。

CL + LF致毒此事。

请参见控制了命令行提示符

I’m trying to make a C program which is prompting the user to type a message from the console and then show that message the user has typed, as in the following example:

C:>promptTest
type your message >>>>  test
typed :  test

C:>

This is my code:

#include <stdio.h>
#include <string.h>
int main(){
    char msg[32];
    printf("type your message >>>>\t");
    fgets(msg,sizeof(msg),stdin);
    msg[strlen(msg) - 1] = '\0';
    printf("typed :  %s\n",msg);
    return 0;
}

It can be built on both Windows7 and CentOS and it can be run normally on Windows like above.

However, it can’t be run on CentOS. I mean nothing accepts any message from the prompt like below:

$ ./promptTest
type your message >>>>  test

typed :  
$ 

How can I fix this?

Here is information about my machine.

$ cat /etc/redhat-release
CentOS release 6.4 (Final)
$ 
$ arch
x86_64
$ 

解决方案

I found the solution.

CL+LF coused this matter.

see Controlling prompt for command line

这篇关于与fgets()函数不能接受命令提示信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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