处理输入流中不需要的字符的准则是什么? [英] What's the guideline for dealing with unwanted chars in input stream?

查看:59
本文介绍了处理输入流中不需要的字符的准则是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ *

我们什么时候应该担心输入流中不需要的字符?我们可以在这种行为中预测这种行为,并在调试之前阻止它吗?

测试?处理它的指导原则是什么?


如下面第21行所示,我应该删除

输入流中不需要的字符时间。我是否会错过其他可能出现的错误

i / o有时会在其他地方发生?并且

欢迎您对以下代码表示赞赏。谢谢。

* /

1 #define STRLEN 200

2

3 int main(int argc,char * argv [])

4 {

5 int ret = 0;

6 char cust [STRLEN] = {''\ 0''};

7 char dest [STRLEN] = {''\ 0''};

8 char flight =''\ 0'';

9 char hotel =''\ 0'';

10 br / >
11 printf(客户名称:);

12获得(cust);

13 printf(" Destination:");

14得到(dest);

15

16 printf(将会有航班:);

17 flight = getchar();

18 printf(酒店可用:);

19

20 / *现在删除输入流中不需要的字符* /

21 while(getchar()!=''\ n''); //预期的空循环体

22 hotel = getchar();

23

24 printf(" \ n- summary -\\ \\ n");

25 printf(客户名称\t:%s \ n,cust);

26 printf(" Destination \ t:%s \ n",dest);

27 printf(可用航班\t:%c \ n,航班);

28 printf(酒店可用\:%c \ n,酒店);

29

30返回ret;

31}

32

/*
When should we worry about the unwanted chars in input stream? Can we
predicate this kind of behavior and prevent it before debugging and
testing? What''s the guideline for dealing with it?

As shown below line #21, I should remove the unwanted characters in
input stream there at that time. Do I miss some other possible errors
in i/o which will happen to occur sometimes in other places? And
welcome your kind comments on following the code, thank you.
*/
1 #define STRLEN 200
2
3 int main(int argc, char *argv[])
4 {
5 int ret = 0;
6 char cust[STRLEN] = {''\0''};
7 char dest[STRLEN] = {''\0''};
8 char flight = ''\0'';
9 char hotel = ''\0'';
10
11 printf("Customer name: ");
12 gets(cust);
13 printf("Destination: ");
14 gets(dest);
15
16 printf("Will flight be available: ");
17 flight = getchar();
18 printf("Will hotel be available: ");
19
20 /* remove unwanted chars in input stream here now */
21 while(getchar() != ''\n''); //intended null loop body
22 hotel = getchar();
23
24 printf("\n- summary -\n");
25 printf("Customer name\t: %s \n" ,cust);
26 printf("Destination\t: %s \n" ,dest);
27 printf("is flight available\t: %c \n", flight);
28 printf("is hotel available\t: %c \n", hotel);
29
30 return ret;
31 }
32

推荐答案

lovecreatesbeauty说:
lovecreatesbeauty said:
/ *
我们什么时候应该担心输入流中不需要的字符?


当你知道它们是不需要的时候。

我们可以在调试之前预测这种行为并防止这种行为吗
测试?处理它的准则是什么?


决定您希望保留的内容以及您希望丢弃的内容。设计一个

算法来区分它们。实现算法。

如下面的第21行所示,我应该删除当时
输入流中不需要的字符。我是否会错过在其他地方偶尔会发生的其他可能的错误?并且欢迎您对以下代码表示赞赏。谢谢。
* /

1 #define STRLEN 200
2
3 int main(int argc,char * argv [])


做得好,你有main()吧。一个好的开始。

4 {
5 int ret = 0;
6 char cust [STRLEN] = {''\ 0''};
7 char dest [STRLEN] = {''\ 0''};
8 char flight =''\ 0'';
9 char hotel =''\ 0'';
10
11 printf(客户名称:);


未定义的行为。你忘了#include< stdio.h>

12得到(cust);


永远,永远,永远,永远,永远不会使用get()。使用fgets代替,因为

它允许您指定

输入可用的存储空间。任何不合适的输入将留在流中等待收集

由下一个从该流中读取的函数。

13 printf(" Destination:" );
14得到(dest);


永远,永远,永远,永远,永远不会使用get()。使用fgets代替,因为

它允许您指定

输入可用的存储空间。任何不合适的输入将留在流中等待收集

由下一个从该流中读取的函数。

15
16 printf(" ;航班是否可用:);
17 flight = getchar();


为什么不在这里使用fgets?

18 printf(酒店可用:);
19
20 / *现在删除输入流中不需要的字符* /
21 while(getchar()!=''\ n''); //预期的空循环体
/*
When should we worry about the unwanted chars in input stream?
When you know they''re unwanted.
Can we
predicate this kind of behavior and prevent it before debugging and
testing? What''s the guideline for dealing with it?
Decide what you wish to keep and what you wish to discard. Devise an
algorithm for distinguishing between them. Implement the algorithm.
As shown below line #21, I should remove the unwanted characters in
input stream there at that time. Do I miss some other possible errors
in i/o which will happen to occur sometimes in other places? And
welcome your kind comments on following the code, thank you.
*/
1 #define STRLEN 200
2
3 int main(int argc, char *argv[])
Well done, you got main() right. A good start.
4 {
5 int ret = 0;
6 char cust[STRLEN] = {''\0''};
7 char dest[STRLEN] = {''\0''};
8 char flight = ''\0'';
9 char hotel = ''\0'';
10
11 printf("Customer name: ");
Undefined behaviour. You forgot to #include <stdio.h>
12 gets(cust);
Never, ever, ever, ever, ever, ever, ever use gets(). Use fgets instead, as
it allows you to specify how much storage space you have available for the
input. Any input that won''t fit will stay in the stream awaiting collection
by the next function to read from that stream.
13 printf("Destination: ");
14 gets(dest);
Never, ever, ever, ever, ever, ever, ever use gets(). Use fgets instead, as
it allows you to specify how much storage space you have available for the
input. Any input that won''t fit will stay in the stream awaiting collection
by the next function to read from that stream.
15
16 printf("Will flight be available: ");
17 flight = getchar();
Why not use fgets here too?
18 printf("Will hotel be available: ");
19
20 /* remove unwanted chars in input stream here now */
21 while(getchar() != ''\n''); //intended null loop body




这不会起作用,因为它会继续阅读和丢弃字符

到和包括第一个非换行,大概应该是

数据。


我建议使用fgets以字符串形式捕获所有数据(现在,那个

是 - 如果你有更多的经验,你可能会设计自己的输入程序,即使它是单字符数据。如果你需要的话就可以从字符串中选择一个字符,这很容易




-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)



This won''t work, because it will keep reading and discarding characters up
to AND INCLUDING the first non-newline, which is presumably supposed to be
data.

I suggest capturing all your data in string form, using fgets (for now, that
is - later you''ll probably devise your own input routine when you have a
lot more experience), even if it''s single-character data. It''s easy enough
to pick a single character out of a string if that''s all you need from it.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


Richard Heathfield写道:
Richard Heathfield wrote:
lovecreatesbeauty说:

....大片...
lovecreatesbeauty said:
.... big snip ...
19
20 / *现在删除输入流中不需要的字符* /
21 while(getchar()!=''\ n''); //预期的null循环体
19
20 /* remove unwanted chars in input stream here now */
21 while(getchar() != ''\n''); //intended null loop body



这不会起作用,因为它会继续阅读和丢弃
字符,包括第一个非换行符,这是<大概应该是数据。



This won''t work, because it will keep reading and discarding
characters up to AND INCLUDING the first non-newline, which is
presumably supposed to be data.




Hunh?它将从流中删除字符,包括''\ n''。

错误是它没有检查EOF,这可能导致

等待相当长。


-

"如果你想通过groups.google.com发布后续帖子,请不要使用

破损的回复链接在文章的底部。点击

" show options"在文章的顶部,然后点击

回复在文章标题的底部。 - Keith Thompson

更多细节见:< http://cfaj.freeshell.org/google/>



Hunh? It will remove chars from the stream, including the ''\n''.
The fault is that it doesn''t check for EOF, which can lead to a
fairly long wait.

--
"If you want to post a followup via groups.google.com, don''t use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>


Chuck F.说:
Chuck F. said:
Richard Heathfield写道:
Richard Heathfield wrote:
lovecreatesbeauty说:
lovecreatesbeauty said:


...大片...


... big snip ...

>
19
20 / *现在删除输入流中不需要的字符* /
21 while(getchar()!=''\ n'') ; //预期的null循环体
19
20 /* remove unwanted chars in input stream here now */
21 while(getchar() != ''\n''); //intended null loop body



这不会起作用,因为它会继续阅读和丢弃
字符,包括第一个非换行符,这是<大概应该是数据。



This won''t work, because it will keep reading and discarding
characters up to AND INCLUDING the first non-newline, which is
presumably supposed to be data.



Hunh?它将从流中删除字符,包括''\ n''。



Hunh? It will remove chars from the stream, including the ''\n''.




糟糕。我很抱歉。我出于某种原因将其读作==。

-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)



Oops. My apologies. I read it as == for some reason.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


这篇关于处理输入流中不需要的字符的准则是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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