473,385 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

EOF in C and OSX

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
10 2362
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
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
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
"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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.