EOF in C and OSX

S

sushi boi

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);
}
 
R

Richard Heathfield

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

TWENTYFIVECHARACTERSTRINGfollowedbycodetocorruptyourparameterpassingmechanismandjumptomyownarbitraryexploit

?
 
S

sushi boi

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'?
 
K

Keith Thompson

sushi boi said:
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.
 
O

osmium

sushi boi said:
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;
}
 
S

sushi boi

... 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
 
S

sushi boi

sushi boi said:
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");printf("Second file\n");
while(fgets(s, 256, stdin))
printf("%s\n", s);
return 0;
}

THANKS EVERYONE!.. thats the line i was lookin for!
 
B

Ben Pfaff

sushi boi said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top