基本的c / o和EOF [英] basic c i/o and EOF

查看:83
本文介绍了基本的c / o和EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我编写的代码,用于计算输入的字符数.I

假设EOF为-1,所以如果我输入-1然后按回车不应该

程序结束?如果我在while循环中输入类似''q'的东西,那么它就会结束循环。


是什么东西?


< code>


#include< stdio.h>


void main(){


long nc;


nc = 0;

while(getchar()!=''EOF'') {

++ nc;

}

printf("%ld \ n",nc);


}


< / code>


谢谢

Dave

Below is the code ive written just to count the characters typed in. I
assumed EOF is -1, so if i type -1 and then press enter shouldnt the
program end? It orks if i put something like ''q'' in the while loop to
end the loop.

what is up?

<code>

#include <stdio.h>

void main() {

long nc;

nc = 0;
while (getchar() != ''EOF'') {
++nc;
}
printf("%ld\n", nc);

}

</code>

thanks
Dave

推荐答案

" Dave" <无***** @ noemail.com> skrev i meddelandet

新闻:bj ********** @ titan.btinternet.com ...
"Dave" <no*****@noemail.com> skrev i meddelandet
news:bj**********@titan.btinternet.com...
以下是我编写的代码输入的字符.I
假设EOF是-1,所以如果我输入-1然后按回车,
程序结束了吗?
Below is the code ive written just to count the characters typed in. I
assumed EOF is -1, so if i type -1 and then press enter shouldnt the
program end?




不,因为当你输入-1时,它不会被存储为一个角色,而是两个

个字符'' - ''和''1'。 -1然而是一个单个字符,而那两个

值并不匹配。


但是,你可以自己做一个测试,并检查一系列字母。

你应该很容易做到。我相信理论是将所有

输入字符放入一个数组中,并检查

数组中的最后两个字符是否等于'' - ''和''1'。


-

Tim Cambrant

< tim at cambrant dot com>



No, because when you type -1 it isn''t stored as a character, but two
characters ''-'' and ''1''. ''-1'' however is a single character, and those two
values doesn''t match.

However, you could do a test yourself, and check for a series of letters.
Should be quite easy for you to do. I believe the theory is to put all the
input characters into an array, and check if the last two characters in the
array is equal to ''-'' and ''1''.

--
Tim Cambrant
<tim at cambrant dot com>


2003年9月11日星期四,格林威治标准时间15:09:58,Tim Cambrant < ti*@cambrant.com.net>写道:
On Thu, 11 Sep 2003 15:09:58 GMT, "Tim Cambrant" <ti*@cambrant.com.net> wrote:
" Dave" <无***** @ noemail.com> skrev i meddelandet
新闻:bj ********** @ titan.btinternet.com ...
"Dave" <no*****@noemail.com> skrev i meddelandet
news:bj**********@titan.btinternet.com...
下面是我编写的代码,用于计算输入的字符数我假设EOF是-1,所以如果我输入-1然后按回车,
程序结束了吗?
Below is the code ive written just to count the characters typed in. I
assumed EOF is -1, so if i type -1 and then press enter shouldnt the
program end?



不,因为当你输入时 - 1它不是作为一个角色存储的,而是两个
字符 - 和1。 ''-1''然而是一个单个字符,而且这两个值不匹配。

然而,你可以自己做一个测试,并检查一系列字母。
你应该很容易做到。我相信理论是将所有
输入字符放入一个数组中,并检查
数组中的最后两个字符是否等于'' - ''和''1'。



No, because when you type -1 it isn''t stored as a character, but two
characters ''-'' and ''1''. ''-1'' however is a single character, and those two
values doesn''t match.

However, you could do a test yourself, and check for a series of letters.
Should be quite easy for you to do. I believe the theory is to put all the
input characters into an array, and check if the last two characters in the
array is equal to ''-'' and ''1''.




或者更好的是,如果您的平台支持它,请输入用于表示输入数据结束信号的密钥或密钥组合

。 />

在基于MSDOS的系统上,这将是^ Z键组合

在基于Unix的系统上,这将是组合产生''eof'的原因'

信号(通常是^ D,但请检查你的stty设置以确定)。

-

Lew Pitcher

企业技术解决方案IT顾问

多伦多道明银行金融集团


(表达的意见是我自己的,而不是我的雇主'') >



Or better yet, if your platform supports it, enter the key or key combination
that signals end-of-input-data.

On MSDOS-based systems, this would be the ^Z key combination
On Unix-based systems, this would be what ever combination generates the ''eof''
signal (typically ^D, but check your stty settings to be sure).
--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers'')


Dave< no ***** @ noemail.com>写道:
Dave <no*****@noemail.com> wrote:
下面是我编写的代码,用于计算输入的字符数.I
假设EOF为-1,所以如果我输入-1然后按Enter键
程序结束了吗?
已经在其他回复中解决过了。

如果我在while循环中输入类似q的内容来结束循环,那么它就可以了。

< code>

#include< stdio.h>

void main(){
int main(void){

long nc;

nc = 0;
while(getchar()!=''EOF''){
^^^^^

非法字符常量,只需写入EOF(在stdio.h中定义)

++ nc;
}
printf("%ld \ n",nc);

返回0;}

< / code>
AFAIK实现定义如何通过

键盘生成EOF(例如Ctr-Z,Ctrl-C,Ctrl-D,...)。
谢谢
戴夫
Below is the code ive written just to count the characters typed in. I
assumed EOF is -1, so if i type -1 and then press enter shouldnt the
program end? Already addressed in the other replies.
It orks if i put something like ''q'' in the while loop to
end the loop.

what is up?

<code>

#include <stdio.h>

void main() { int main( void ) {

long nc;

nc = 0;
while (getchar() != ''EOF'') { ^^^^^
illegal character constant, just write EOF (it is defined in stdio.h)
++nc;
}
printf("%ld\n", nc);
return 0;}

</code> AFAIK it is implementation defined how to produce an EOF via the
keyboard (e.g. Ctr-Z, Ctrl-C, Ctrl-D, ... ).
thanks
Dave




问候


Irrwahn

-

闭上眼睛,按三次逃生。



Regards

Irrwahn
--
Close your eyes and press escape three times.


这篇关于基本的c / o和EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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