C和OSX中的EOF [英] EOF in C and OSX

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

问题描述



我注意到OSx的scanf有些奇怪的行为。出于某种原因

后续scanf语句如果按ctrl

+ d(eof),则在osx中​​停止工作。看看我在说什么..合并并运行这个...打印

语句告诉你会发生什么以及该怎么做:


#include< stdio.h>

#include< string.h>

#include< stdlib.h>


#define STR_LENGTH 25

int main(){

char str1 [STR_LENGTH];

char str2 [STR_LENGTH] ;

int num;

char err [STR_LENGTH];

printf(" type any string:");

scanf("%s",str1);

printf(" type any number:");

scanf("%d" ,& num);

printf(" fName ... press EOF:");

scanf("%s",fName);

printf(期待一个字符串......输入EOF(ctrl + d)..:");

scanf("%s",str2);

printf(\ nmeant要求更多输入:\ n);

scanf("%s",err);

printf(看看我的意思!\ n);

返回(0);

}


ive noticed some weird behavior with OSx''s scanf. For some reason
subsequent scanf statements stops working in osx if you press ctrl
+d(eof). to see what i''m on about.. combile and run this.. the print
statements tell u what to expect and what to do:

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

#define STR_LENGTH 25

int main(){
char str1[STR_LENGTH];
char str2[STR_LENGTH];
int num;
char err[STR_LENGTH];
printf("type any string: ");
scanf("%s",str1);
printf("type any number: ");
scanf("%d",&num);
printf("fName... press EOF: ");
scanf("%s",fName);
printf("expecting a string... Type EOF(ctrl+d).. : ");
scanf("%s",str2);
printf("\nmeant to ask for more input: \n");
scanf("%s",err);
printf("see what i mean!\n");
return (0);
}

推荐答案

寿司boi说:
sushi boi said:

hi,

我注意到OSx的scanf有些奇怪的行为。出于某种原因

后续scanf语句如果按ctrl

+ d(eof),则在osx中​​停止工作。

ive noticed some weird behavior with OSx''s scanf. For some reason
subsequent scanf statements stops working in osx if you press ctrl
+d(eof).



原因是你已经告诉电脑那个

流没有更多的数据了。那么为什么还要费心去收集呢?

The reason is that you''ve told the computer there''s no more data on that
stream. So why would it bother to try to gather any more?


看看我在说些什么...合并并运行这个...打印
语句告诉你期待什么和做什么:


#include< stdio.h>

#include< string。 h>

#include< stdlib.h>


#define STR_LENGTH 25

int main() {

char str1 [STR_LENGTH];

char str2 [STR_LENGTH];

int num;

char err [STR_LENGTH];

printf(" type any string:");

scanf("%s",str1);
to see what i''m on about.. combile and run this.. the print
statements tell u what to expect and what to do:

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

#define STR_LENGTH 25

int main(){
char str1[STR_LENGTH];
char str2[STR_LENGTH];
int num;
char err[STR_LENGTH];
printf("type any string: ");
scanf("%s",str1);



阻止我打字的原因


TWENTYFIVECHARACTERSTRING关注来自codetocorruptyo urparameterpassingmechanismandjumptomyownarbitrary exploit




-

Richard Heathfield

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

电子邮件:rjh在上述域名, - www。

What''s to stop me typing

TWENTYFIVECHARACTERSTRINGfollowedbycodetocorruptyo urparameterpassingmechanismandjumptomyownarbitrary exploit

?

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


是啊..好点..
<我想问的原因是因为在大学里,他们希望我们制作一个

计划来执行以下操作:


1)

a。要求名字。

b。要求姓氏。

c。要求年龄的人。

d。将人员细节放入一系列结构中。

e。得到另一个人的详细信息,直到EOF作为名字。

2)

a。询问用户要搜索的名称。

b。搜索结构数组中是否存在名称。

c。再次启动,直到EOF作为起始字符串给出。


i编程程序,但是当它在我的mac上运行它

将无法工作。

然而它在我的教师的计算机上工作。

也是..如果我在大学的帐户里进行...程序运行正常。


有趣的是我的讲师声称它在她身上运作得非常好

mac

如果知道如何解决这个问题吗? br />
yea.. good point..

the reason that i''m asking is because in uni they wanted us to make a
program that does the following:

1)
a. asks for first name.
b. asks for last name.
c. asks for persons age.
d. puts the persons details into an array of structs.
e. gets another persons details until EOF is given as first name.
2)
a. asks user for a name to search for.
b. searches the array of structs for a occurance of a name.
c. starts again until EOF is given as start string.

i coded the program but when it came to running it on my mac it
wouldnt work.
however it worked on my faculty''s computers.
ALSO.. if i ssh into my account at uni.. the program works fine.

funny thing is my lecturer claims that it works perfectly well on her
mac
does anyone kno if theres a way around this ''delemma''?


sushi boi< su ******** @ gmail.comwrites:
sushi boi <su********@gmail.comwrites:

已经注意到OSx的scanf有些奇怪的行为。出于某种原因

后续scanf语句如果按ctrl

+ d(eof),则在osx中​​停止工作。
ive noticed some weird behavior with OSx''s scanf. For some reason
subsequent scanf statements stops working in osx if you press ctrl
+d(eof).



[snip]


是的。 " EOF"代表文件结束。这就是它* * * * b
$ b。我认为不同的系统在遭遇时表现不同

交互式输入流上的文件结束。我相信你描述的行为

是正确的。如果你想清除文件结尾

条件,你可以打电话给更清楚的人。


-

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

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

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

- Antony Jay和Jonathan Lynn,是部长

[snip]

Yes. "EOF" stands for End Of File. That''s what it''s *supposed* to
do.

I think different systems behave differently on encountering
end-of-file on an interactive input stream. I believe the behavior
you describe is correct. If you want to clear the end-of-file
condition, you can call clearerr.

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


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

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