By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

EOF in C and OSX

P: n/a
hi,
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);
}

Jun 11 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
sushi boi said:
hi,
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?
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);
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.
Jun 11 '07 #2

P: n/a
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'?

Jun 11 '07 #3

P: n/a
sushi boi <su********@gmail.comwrites:
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]

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"
Jun 11 '07 #4

P: n/a
"sushi boi" writes:
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:
<snip>

Try this.

#include <stdio.h>

int main()
{
char s[256];

printf("Enter some strings and then signal EOF\n");
while(fgets(s, 256, stdin) )
printf("%s\n", s);
printf("EOF detected\n");
clearerr(stdin); /* restore "good" state */
printf("Second file\n");
while(fgets(s, 256, stdin))
printf("%s\n", s);
return 0;
}
Jun 11 '07 #5

P: n/a
On Jun 12, 2:52 am, Keith Thompson <k...@mib.orgwrote:
... you can call clearerr.
AH... sounds like what i'm looking for...
the manual says i need to give it a File* as input...
is there a equivilant to this function for a users input directly into
command

Jun 11 '07 #6

P: n/a
On Jun 12, 3:03 am, "osmium" <r124c4u...@comcast.netwrote:
"sushi boi" writes:
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:

<snip>

Try this.

#include <stdio.h>

int main()
{
char s[256];

printf("Enter some strings and then signal EOF\n");
while(fgets(s, 256, stdin) )
printf("%s\n", s);
printf("EOF detected\n");
>> clearerr(stdin); /* restore "good" state */
printf("Second file\n");
while(fgets(s, 256, stdin))
printf("%s\n", s);
return 0;
}
THANKS EVERYONE!.. thats the line i was lookin for!

Jun 11 '07 #7

P: n/a
sushi boi <su********@gmail.comwrites:
On Jun 12, 2:52 am, Keith Thompson <k...@mib.orgwrote:
>... you can call clearerr.

AH... sounds like what i'm looking for...
the manual says i need to give it a File* as input...
is there a equivilant to this function for a users input directly into
command
You might be looking for clearerr(stdin).
--
Ben Pfaff
http://benpfaff.org
Jun 11 '07 #8

P: n/a
sushi boi wrote:
>
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:
So? What do you expect after EOF?

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 11 '07 #9

P: n/a
sushi boi wrote:
>
.... snip ...
>
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.
The reason is that systems vary as to whether EOF is permanent or
simply a one-time signal. The better systems have it permanent.

Please include sufficient quotes in your replies so that your
article is understandable alone.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 11 '07 #10

P: n/a
On 11 Jun 2007 at 22:52, CBFalconer wrote:
Please include sufficient quotes in your replies so that your
article is understandable alone.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com
Please use a netiquette-compliant signature.

--
CBFalconer: breathtaking hypocrite or insidious troll?
Jun 14 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.