Linux上的标准输入流行为 [英] Standard input stream behaviour on Linux

查看:45
本文介绍了Linux上的标准输入流行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个完全可移植的C程序(或者至少我认为我这样做)。它可以在Windows上运行,但在Linux上出现故障。我怀疑有一些我不知道标准输入流导致问题的原因是什么。

这是我最初编写程序的方式:


#include< stdio.h>

#include< string。 h>


void TrimNewLineOffEnd(char * const str)

{

str [strlen(str) - 1] = 0 ;

}


int main(无效)

{

char buf [20];


未签名年龄,兄弟姐妹;


printf(你几岁?);


scanf("%u"& age);


printf(你有几个兄弟姐妹?);


scanf("%u",& siblings);


printf("你叫什么名字?);


fgets(buf,15,stdin);


TrimNewLineOffEnd(buf);


printf(" \ n \\ n你的名字是%s,你年纪不大,你还有e%u

兄弟姐妹!\ n \ n",

buf,年龄,兄弟姐妹);


返回0 ;

}


这个原始代码在Windows或Linux上无法正常工作。

在两个系统上,当控制达到fgets时,用户没有机会输入他们的名字;相反,fgets立即返回

一个空字符串。


为了解决Windows上的这个问题,我输入了fflush(stdin)。在打电话给fgets之前,

。这解决了问题,程序

按预期工作。但这并不适用于Linux。


我做错了什么?


I have a fully-portable C program (or at least I think I do). It works
fine on Windows, but malfunctions on Linux. I suspect that there''s
something I don''t know about the standard input stream that''s causing
the problem.

Here''s how I wrote the program originally:

#include <stdio.h>
#include <string.h>

void TrimNewLineOffEnd(char *const str)
{
str[strlen(str) - 1] = 0;
}

int main(void)
{
char buf[20];

unsigned age, siblings;

printf("What age are you? ");

scanf("%u",&age);

printf("How many siblings do you have? ");

scanf("%u", &siblings);

printf("What''s your name? ");

fgets(buf,15,stdin);

TrimNewLineOffEnd(buf);

printf("\n\nYour name is %s, you''re %u years old and you have %u
siblings!\n\n",
buf,age,siblings);

return 0;
}

This original code didn''t work as intended on either Windows or Linux.
On both systems, when control reached "fgets", the user wasn''t given a
chance to enter their name; instead, fgets returned immediately with
an empty string.

To remedy this problem on Windows, I put in "fflush(stdin)" right
before the call to "fgets". This fixed the problem and the program
worked as intended. This didn''t work on Linux however.

What am I doing wrong?

推荐答案

Tomás óhéilidhe写道:
Tomás ó héilidhe wrote:

>

这个原始代码在Windows或Linux上无法正常工作。

在两个系统上,当控制达到fgets时,用户没有机会输入他们的名字;相反,fgets立即返回

一个空字符串。


为了解决Windows上的这个问题,我输入了fflush(stdin)。在打电话给fgets之前,

。这解决了问题,程序

按预期工作。但这并不适用于Linux。


我做错了什么?
>
This original code didn''t work as intended on either Windows or Linux.
On both systems, when control reached "fgets", the user wasn''t given a
chance to enter their name; instead, fgets returned immediately with
an empty string.

To remedy this problem on Windows, I put in "fflush(stdin)" right
before the call to "fgets". This fixed the problem and the program
worked as intended. This didn''t work on Linux however.

What am I doing wrong?



调用fflush(stdin)。 fflush only(portably)适用于输出流。


-

Ian Collins

Calling fflush(stdin). fflush only (portably) works on output streams.

--
Ian Collins


Tomás óhéilidhe< t ... @ lavabit.comwrote:
Tomás ó héilidhe <t...@lavabit.comwrote:

我有一个完全可移植的C程序(或者至少我认为我是

do)。它在Windows上工作正常,但在Linux上出现故障。

我怀疑有一些我不知道的关于

标准输入流的信息。造成这个问题。
I have a fully-portable C program (or at least I think I
do). It works fine on Windows, but malfunctions on Linux.
I suspect that there''s something I don''t know about the
standard input stream that''s causing the problem.



你还没看过常见问题。

You haven''t read the FAQ.


这就是我编写程序的方式最初:


#include< stdio.h>

#include< string.h>


void TrimNewLineOffEnd(char * const str)

{

* * str [strlen(str) - 1] = 0;

}
Here''s how I wrote the program originally:

#include <stdio.h>
#include <string.h>

void TrimNewLineOffEnd(char *const str)
{
* * str[strlen(str) - 1] = 0;
}



这假设有换行符。

This assumes there was a newline.


int main(void)

{

* * char buf [20];

* *未签名年龄,兄弟姐妹;


* * printf("你几岁?);
int main(void)
{
* * char buf[20];
* * unsigned age, siblings;

* * printf("What age are you? ");



如果你的提示没有

a换行,你应该刷新标准输出。

You should flush stdout if your prompt doesn''t have
a newline.


* * scanf("%u",& age);
* * scanf("%u",&age);



你应该真正检查返回值。

You should really check the return value.


* * printf("多少兄弟姐妹做你有吗?");

* * scanf("%u",& siblings);
* * printf("How many siblings do you have? ");
* * scanf("%u", &siblings);



没有理由让%u转换输入数字后面的换行符


There is no reason for %u to convert the newline
that follows the entered number.


* * printf("你叫什么名字?");

* * fgets(buf,15,stdin);
* * printf("What''s your name? ");
* * fgets(buf,15,stdin);



更强大的是fgets(buf,sizeof buf,stdin)

More robust is fgets(buf, sizeof buf, stdin)


* * TrimNewLineOffEnd(buf) ;
* * TrimNewLineOffEnd(buf);



最后一行不一定是换行符

输入。


< snip>

There won''t necessarily be a newline on the last line
of input.

<snip>


为了解决Windows上的这个问题,我输入了

" fflush(stdin)"
To remedy this problem on Windows, I put in
"fflush(stdin)"



Google clc对fflush stdin的评论。

Google clc for comments on fflush stdin.


就在调用fgets之前。这修复了

问题,程序按预期工作。
right before the call to "fgets". This fixed the
problem and the program worked as intended.



但是它不可移植。

But it wasn''t portable.


但这并不适用于Linux。
This didn''t work on Linux however.



未定义的行为很少适用于所有系统。


-

彼得


Tomásóhéilidhe写道:
Tomás ó héilidhe wrote:

我有一个完全可移植的C程序(或者至少我认为我这样做) 。它可以在Windows上运行,但在Linux上出现故障。我怀疑有一些我不知道标准输入流导致问题的原因是什么。

这是我最初编写程序的方式:


#include< stdio.h>

#include< string。 h>


void TrimNewLineOffEnd(char * const str)

{

str [strlen(str) - 1] = 0 ;

}
I have a fully-portable C program (or at least I think I do). It works
fine on Windows, but malfunctions on Linux. I suspect that there''s
something I don''t know about the standard input stream that''s causing
the problem.

Here''s how I wrote the program originally:

#include <stdio.h>
#include <string.h>

void TrimNewLineOffEnd(char *const str)
{
str[strlen(str) - 1] = 0;
}



请参阅Peter Nilsson和Ian Collins的回复。正如Nilsson

指出的那样,这个函数会丢掉最后一个角色,无论是否是
它是否是换行符。 (如果

参数是空字符串,它会*真的*行为异常!)如果你想删除换行符

(如果存在)并保持字符串不变,否则安全地执行
,并坚持只用一行来做,尝试


str [strcspn(str," \ n")] =''\''';


尊重第七条诫命!

http://www.lysator.liu.se/c/ten-commandments.html

-
Er ********* @ sun.com

See the responses by Peter Nilsson and Ian Collins. As Nilsson
points out, this function will lop off the final character whether
it''s a newline or not. (And it will *really* misbehave if the
argument is the empty string!) If you want to remove a newline
if present and leave the string unchanged otherwise, and do it
safely, and still insist on doing it with just one line, try

str[ strcspn(str, "\n") ] = ''\0'';

Honor the Seventh Commandment!

http://www.lysator.liu.se/c/ten-commandments.html

--
Er*********@sun.com


这篇关于Linux上的标准输入流行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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