C fgets()函数无法接受来自命令提示符的消息

C fgets()函数无法接受来自命令提示符的消息,c,centos,fgets,C,Centos,Fgets,我正在尝试制作一个C程序,它会提示用户从控制台键入一条消息,然后显示用户键入的消息,如下例所示: C:>promptTest type your message >>>> test typed : test C:> 这是我的代码: #include <stdio.h> #include <string.h> int main(){ char msg[32]; printf("type your message &

我正在尝试制作一个C程序,它会提示用户从控制台键入一条消息,然后显示用户键入的消息,如下例所示:

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

C:>
这是我的代码:

#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;
}
我怎样才能解决这个问题

这是关于我的机器的信息

$ cat /etc/redhat-release
CentOS release 6.4 (Final)
$ 
$ arch
x86_64
$ 
strlen返回size\u t not int

在使用%d的64位IXish系统上打印大小\u t可能会失败,因为前者最可能是64位,而后者最可能是32位

所以至少在64位IXish系统上,而不是

printf("### buf(%d) = %s\n",strlen(buf),buf);
printf("### message(%d) = %s\n",strlen(message),message);

作为一个丑陋的解决方法,你也可以把strlen降到int

但是,此解决方案假定字符串不再超过INT_MAX。

我找到了解决方案

CL+LF对这件事进行了调查


请参见在CentOS为我工作。您在这两个版本中使用相同的源吗?意味着在windows中的第一个输出中,如buf=和您在centOS中的尝试输出不同,如buf1=示例文本错误,我对其进行了编辑。您的代码仍然与示例输出不一致。strlen返回size\u t,但带有%d的printf需要int。@opd no,即为buf[strlenbuf]。我猜OP想删除“\n”。很抱歉,我绑定了您的代码,但它不起作用。不接受消息。请使用符号选项-g为gcc编译代码,然后使用调试器gdb运行代码以查看实际情况。您可能希望通过添加选项-Wall-Wextra-pedantic来启用所有警告。它显示未使用的参数“agrc”,未使用的参数“argv”和控件可能会到达非无效函数的末尾。我已尝试删除这些警告。我在问题中编辑了代码。现在没有任何警告。
printf("### buf(%d) = %s\n",strlen(buf),buf);
printf("### message(%d) = %s\n",strlen(message),message);
printf("### buf(%zu) = %s\n",strlen(buf),buf);
printf("### message(%zu) = %s\n",strlen(message),message);
printf("### buf(%d) = %s\n", (int) strlen(buf),buf);
printf("### message(%d) = %s\n", (int) strlen(message),message);