如果EOF = -1,不能有效的字符== EOF并导致问题? [英] if EOF = -1, can't a valid character == EOF and cause problems?

查看:78
本文介绍了如果EOF = -1,不能有效的字符== EOF并导致问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于EOF(我使用的

实现中的(int)-1)。在我的实现上键入char

也是''签名''。


如何返回输入库函数

如果从输入文件中读取ASCII 255

之类的东西,可以正确使用EOF吗? 255将返回-1,

并将与EOF进行比较并导致问题。


声明要保留int的重点是什么从这些函数中返回

的值,当一个char(在我的实现中)

可以表示-1(我的表示上的EOF)也是?


示例;


char c_char;

while((c_char = getchar())!= EOF)

{/ *某事* /}


的工作方式与*非常相似*


int c_int;

while((c_int = getchar())!= EOF)

{/ *某事* /}

我不是在抱怨
(int)EOF vs.(int)(Ascii 255返回为-1)

但是想知道为什么人们建议你从中获取

的返回值函数如getchar进入

一个int,当int无法帮助我们摆脱

这个模糊的情况?


我试图研究这个死亡并包裹我的

介意一个可能的答案,但我失败了

悲惨。我需要有人帮我打开

闪烁的灯泡。


非常感谢提前

解决方案

Kobu< ko ******** @ gmail.com>潦草地写道:

我的问题是关于EOF(我使用的
实现中的(int)-1)。在我的实现上键入char
也是''签名''。
如果从输入文件中读取ASCII 255
之类的东西,返回的输入库函数是如何正确使用的呢? 255将返回-1,
并将与EOF进行比较并导致问题。
当一个char(在我的实现中)
可以表示-1(我的表示上的EOF)时,从这些函数声明一个int来保存返回值的重点是什么?也?


您刚回答了自己的问题。函数返回int

正是因为需要区分-1和255.

示例;
char c_char;
while((c_char = getchar())!= EOF)
{/ * something * /}
将像* *一样工作:
int c_int;
while((c_int = getchar())!= EOF)
{/ * something * /}


不,它赢了' T。第二个版本将更好地工作。你看,

虽然作为一个有符号的字符,255与-1相同,作为一个有符号的int,

255是255,-1是-1,从不twain应该会见。

我不是在抱怨
(int)EOF与(int)(Ascii 255返回为-1)
但是想知道为什么人们狡猾地从getchar这样的函数中获取
返回值为int,当int无法帮助我们摆脱这个模糊的情况?


但它确实有帮助。

我试图研究这个死亡并将我的思想包裹在一个可能的答案中,但我失败了。我需要有人帮我打开闪烁的灯泡。
非常感谢提前




我希望我能得到帮助。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰-------- \

\ ------------------------------------------- -------------规则! -------- /

我们怎样才能用性来获得我们想要的东西?性是我们想要的。

- Frasier Crane博士


2005年3月3日13:10:56 -0800," &甲部QUOT; < KO ******** @ gmail.com>写道:

我的问题是关于EOF(我使用的
实现中的(int)-1)。在我的实现上键入char
也是''已签名'。

返回的输入库函数如何?如果像ASCII 255那样可以正确使用EOF br />是从输入文件中读取的吗? 255将返回-1,
并将与EOF进行比较并导致问题。

声明一个int来保存返回值的重点是什么?这些函数,当一个char(在我的实现中)
可以表示-1(我的表示上的EOF)吗?




在整数和字符不一样

长度,你会发现char 255显示为整数255

和-1显示为-1。


这是一个广泛使用的面试问题,请仔细阅读。


还学习如何在不使用临时的情况下交换字节,如何使用

实现atoi()以及如何撤销列表,你大部分都在那里!


现在,有5名海盗......


"&甲部QUOT; < KO ******** @ gmail.com>写道:

我的问题是关于EOF(我使用的
实现中的(int)-1)。在我的实现上键入char
也是''已签名'。

返回的输入库函数如何?如果像ASCII 255那样可以正确使用EOF br />是从输入文件中读取的吗? 255将返回-1,
并将与EOF进行比较并导致问题。

声明一个int来保存返回值的重点是什么?这些函数,当一个char(在我的实现中)
可以表示-1(我的表示上的EOF)吗?

示例;

char c_char;
while((c_char = getchar())!= EOF)
{/ *某事* /}
将像*一样*工作:

int c_int;
while((c_int = getchar())!= EOF)
{/ * something * /}




getchar ()返回stdin *中的下一个字符作为unsigned char *

转换为int。如果读取的字符值为255,则getchar()

将返回255,即使签署了纯字符。在文件结束时,

getchar()返回不同的值EOF(系统上的-1)。


如果指定了getchar的结果()到一个类型为

char的变量,你就失去了区分255和-1的能力。


(还有一个潜在的问题在异国情调的系统中,char和
int的大小相同,但你几乎肯定不必担心

。)

-

Keith Thompson(The_Other_Keith) ks***@mib.org < ; http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


My question is about EOF (which is (int)-1 on the
implementation I use). Type char on my implementation
is ''signed'' too.

How is it that the input library functions that return
EOF can be used properly if something like ASCII 255
is read from the input file? 255 would be returned as -1,
and would be compared against EOF and cause problems.

What''s the point of declaring a int to hold the return
value from these functions, when a char(on my implementation)
can represent a -1(EOF on my representation) also?

Example;

char c_char;
while( (c_char = getchar()) != EOF )
{ /* something */ }

will work just as *badly* as:

int c_int;
while( (c_int= getchar()) != EOF )
{ /* something */ }
I''m not complaining about the AMBIGUITY of
(int)EOF vs.(int)(Ascii 255 returned as -1)
but wondering why people suggent capturing the
return value from functions like getchar into
an int, when the int doesn''t help us get rid
of this ambiguous case?

I''ve tried to research this to death and wrap my
mind around a possible answer, but I have failed
miserably. I need someone to help me turn on the
flickering lightbulb.

Much thanks in advance

解决方案

Kobu <ko********@gmail.com> scribbled the following:

My question is about EOF (which is (int)-1 on the
implementation I use). Type char on my implementation
is ''signed'' too. How is it that the input library functions that return
EOF can be used properly if something like ASCII 255
is read from the input file? 255 would be returned as -1,
and would be compared against EOF and cause problems. What''s the point of declaring a int to hold the return
value from these functions, when a char(on my implementation)
can represent a -1(EOF on my representation) also?
You just answered your own question. The functions return int
precisely because of the need to distinguish between -1 and 255.
Example; char c_char;
while( (c_char = getchar()) != EOF )
{ /* something */ } will work just as *badly* as: int c_int;
while( (c_int= getchar()) != EOF )
{ /* something */ }
No, it won''t. The second version will work much better. You see,
although as a signed char, 255 is the same as -1, as a signed int,
255 is 255 and -1 is -1, and never the twain shall meet.
I''m not complaining about the AMBIGUITY of
(int)EOF vs.(int)(Ascii 255 returned as -1)
but wondering why people suggent capturing the
return value from functions like getchar into
an int, when the int doesn''t help us get rid
of this ambiguous case?
But it does help.
I''ve tried to research this to death and wrap my
mind around a possible answer, but I have failed
miserably. I need someone to help me turn on the
flickering lightbulb. Much thanks in advance



I hope I have been of help.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"How can we possibly use sex to get what we want? Sex IS what we want."
- Dr. Frasier Crane


On 3 Mar 2005 13:10:56 -0800, "Kobu" <ko********@gmail.com> wrote:

My question is about EOF (which is (int)-1 on the
implementation I use). Type char on my implementation
is ''signed'' too.

How is it that the input library functions that return
EOF can be used properly if something like ASCII 255
is read from the input file? 255 would be returned as -1,
and would be compared against EOF and cause problems.

What''s the point of declaring a int to hold the return
value from these functions, when a char(on my implementation)
can represent a -1(EOF on my representation) also?



In the case where integers and characters are not the same
length, you''ll find that char 255 appears as an integer 255
and -1 appears as -1.

This is a widely used interview question, so read up on it.

Also learn how to swap bytes without using a temporary, how
to implement atoi() and how to reverse a list and you''re mostly there!

Now, there are 5 pirates ....


"Kobu" <ko********@gmail.com> writes:

My question is about EOF (which is (int)-1 on the
implementation I use). Type char on my implementation
is ''signed'' too.

How is it that the input library functions that return
EOF can be used properly if something like ASCII 255
is read from the input file? 255 would be returned as -1,
and would be compared against EOF and cause problems.

What''s the point of declaring a int to hold the return
value from these functions, when a char(on my implementation)
can represent a -1(EOF on my representation) also?

Example;

char c_char;
while( (c_char = getchar()) != EOF )
{ /* something */ }

will work just as *badly* as:

int c_int;
while( (c_int= getchar()) != EOF )
{ /* something */ }



getchar() returns the next character from stdin *as an unsigned char*
converted to int. If the character read has the value 255, getchar()
will return 255, even if plain char is signed. At end-of-file,
getchar() returns the distinct value EOF (-1 on your system).

If you assign the result of getchar() to a variable of type
char, you lose the ability to distinguish between 255 and -1.

(There''s still a potential problem on exotic systems where char and
int are the same size, but you almost certainly don''t need to worry
about that.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于如果EOF = -1,不能有效的字符== EOF并导致问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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