473,382 Members | 1,204 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,382 software developers and data experts.

Detecting EOF

Hello,

I'm trying to detect the EOF for scanf. But for some reason I can't get
it to trigger. What am I doing wrong? Thanks.

int puzzle[9][9]; // Puzzle data structure
int i, j, count; // Iteration variables
int temparr[81];
char x;
int test;
count = 0;
while (count < 82){

scanf("%c", &x);
if (x == EOF) {
printf("FUCK!");
printf("%d", count);
return 0;
}
test = x;
if (test >= 48 && test <= 57) {
test = test - 48;
temparr[count]= test;
count++;
}
}

Oct 25 '06 #1
3 6306

"jobo" <jo*****@gmail.comwrote in message
news:11*********************@i3g2000cwc.googlegrou ps.com...
Hello,

I'm trying to detect the EOF for scanf. But for some reason I can't get
it to trigger. What am I doing wrong? Thanks.

int puzzle[9][9]; // Puzzle data structure
int i, j, count; // Iteration variables
int temparr[81];
char x;
int test;
count = 0;
while (count < 82){

scanf("%c", &x);
if (x == EOF) {
printf("FUCK!");
printf("%d", count);
return 0;
}
test = x;
if (test >= 48 && test <= 57) {
test = test - 48;
temparr[count]= test;
count++;
}
}
From the manpage of scanf:

These functions return the number of input items assigned,
which can be fewer than provided for, or even zero, in the
event of a matching failure. Zero indicates that, while
there was input available, no conversions were assigned;
typically this is due to an invalid input character, such
as an alphabetic character for a `%d' conversion. The
value EOF is returned if an input failure occurs before
any conversion such as an end-of-file occurs. If an error
or end-of-file occurs after conversion has begun, the num-
ber of conversions which were successfully completed is
returned.

Also, EOF cannot be represented with a char, so the
return value of scanf has to be assigned to an int.

So, to check for EOF you should try something like this

char x;
int r ;
r = scanf("%c", &x);
if (r == EOF) {
....
}
Oct 25 '06 #2
On Wed, 2006-10-25 at 00:46 -0700, jobo wrote:
Hello,

I'm trying to detect the EOF for scanf. But for some reason I can't get
it to trigger. What am I doing wrong? Thanks.
First off, excellent job formatting. One minor issue: you shouldn't
use // comments on Usenent because they'll eventually wrap over and
become syntax errors. I've changed your code in that respect.
int puzzle[9][9]; /* Puzzle data structure */
Hey, hey hey! Where did you start main()? You need to post
a /compilable/ snippit.
int i, j, count; /* Iteration variables */
int temparr[81];
Magic numbers make me nervous. (At the very least, comment in that it's
an array as large as puzzle[][]).
char x;
int test;
count = 0;
while (count < 82){
There's that magic number again! Use <= 81 to be consistent, but you
should really define some constants at the start of your program.
>
scanf("%c", &x);
Just use getchar() for this:
x = getchar();
Much simpler, no?
if (x == EOF) {
Here's your problem: EOF is not necessarily in the range of char.
(Specifically, if char is unsigned, it is not, but if char is signed
[which is likely], it may be.) Define x as int so that you are
guaranteed to be able to hold EOF.
printf("FUCK!");
Having a little trouble with this one, eh? ;-)
printf("%d", count);
return 0;
}
test = x;
if (test >= 48 && test <= 57) {
What is this test? It looks like some character test dependant on ASCII.
#include <ctype.hand use isalpha(), isdigit() and friends instead.
test = test - 48;
Ditto here. I think that you're trying to convert a character digit to a
numberic one. In that case, try
test -= '0';
Your intent will be much clearer.
temparr[count]= test;
count++;
}
}
Where were you using the puzzle[][] array? Or i and j? You should have
just gotten rid of them if they were unnecessary to your post.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

Oct 25 '06 #3
Andrew Poelstra said:
On Wed, 2006-10-25 at 00:46 -0700, jobo wrote:
<snip>
> int temparr[81];
<snip>
>
> char x;
int test;
count = 0;
while (count < 82){

There's that magic number again! Use <= 81 to be consistent, but you
should really define some constants at the start of your program.
I think you meant < 81 or possibly <= 80

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jole | last post by:
Hi I'm writing a program that needs to read from a file. In order for the program to be robust, it should somehow check that the file isn't corrupt, or stuffed in any way. For example, that...
15
by: Jay | last post by:
I'm sure this is a really dumb question, but how do you detect a variable type in Python? For example, I want to know if the variable "a" is a list of strings or a single string. How do I do...
3
by: raptor | last post by:
hi, how to detect opera..it seems that even opera8 doesnt support xmlhttp fully (.i.e. sendRequestHeader). I ask this 'cause opera seems to mimic IE, at least in the preferences ?! I havent...
9
by: D. Shane Fowlkes | last post by:
I'm using SQL Server 2000 and on my page, I'm simply creating a SQLDataReader and filling in Labels with the retrieved (single) record. However, how can I prevent from getting errors when a field...
3
by: regtrashcan | last post by:
I have a webpage that detects whether Shockwave Player is installed and the version number. The javascript/vbscript that I use has worked fine until the latest release of the Shockwave Player. I am...
79
by: VK | last post by:
I wandering about the common proctice of some UA's producers to spoof the UA string to pretend to be another browser (most often IE). Shouldn't it be considered as a trademark violation of the...
5
by: Z.K. | last post by:
In C#, using the StreamReader, how do I detect when you get to the end of line. I am reading a text file using the Read() function and I need to detect the \n\r, but everything I try does not...
1
by: wwwords | last post by:
Is there a general method for detecting that a user has changed the record currently visible on a form, whether this is by hitting PgUp or PgDn or clicking on a navigation button, even if no change...
15
by: RobG | last post by:
When using createEvent, an eventType parameter must be provided as an argument. This can be one of those specified in DOM 2 or 3 Events, or it might be a proprietary eventType. My problem is...
1
by: davidson1 | last post by:
Hello Friends, I am using HP Laptop.I have a MP3 Player.I used to charge it using my laptop(Windows XP).But Nowadays it is not detecting and charging whenever I connect MP3 Player...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.