Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > EOF Question

Reply
Thread Tools

EOF Question

 
 
jobo
Guest
Posts: n/a
 
      10-25-2006
Hey,

I'm trying to detect the EOF of the file I pass to my scanf function.
Does anyone know why my if statement isn't triggering? 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("END!");
printf("%d", count);
return 0;
}
test = x;
if (test >= 48 && test <= 57) {
test = test - 48;
temparr[count]= test;
count++;
}
}

 
Reply With Quote
 
 
 
 
Simon Biber
Guest
Posts: n/a
 
      10-25-2006
jobo wrote:
> Hey,
>
> I'm trying to detect the EOF of the file I pass to my scanf function.
> Does anyone know why my if statement isn't triggering? 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) {


If your scanf encounters EOF, it has failed to get a character.
Therefore it does not store anything in x, but rather returns 0 to
indicate that none of the fields were matched. You should be testing the
returned value of scanf, not the value of x after scanf has run.

if(scanf("%c", &x) == 0)
{
printf("END! %d\n", count);
return 0;
}


> printf("END!");
> printf("%d", count);
> return 0;
> }
> test = x;
> if (test >= 48 && test <= 57) {
> test = test - 48;


This code assumes an ASCII-based character set. It should be replaced by
portable code (which requires #include <ctype.h>).

if(isdigit((unsigned char)test)) {
test = test - '0';
...

> temparr[count]= test;
> count++;
> }
> }


--
Simon.
 
Reply With Quote
 
 
 
 
Simon Biber
Guest
Posts: n/a
 
      10-25-2006
Simon Biber wrote:
> jobo wrote:
>> scanf("%c", &x);
>> if (x == EOF) {

>
> If your scanf encounters EOF, it has failed to get a character.
> Therefore it does not store anything in x, but rather returns 0 to
> indicate that none of the fields were matched. You should be testing the
> returned value of scanf, not the value of x after scanf has run.
>
> if(scanf("%c", &x) == 0)
> {
> printf("END! %d\n", count);
> return 0;
> }


Replying to myself here: sorry, I misremembered what scanf returns. If
an end-of-file condition or other input failure is encountered before
any conversion occurs, it will actually return EOF. So change that to:

if(scanf("%c", &x) == EOF)

or even:

if(scanf("%c", &x) != 1)

--
Simon.
 
Reply With Quote
 
Andrew Poelstra
Guest
Posts: n/a
 
      10-25-2006
On Wed, 2006-10-25 at 00:52 -0700, jobo wrote:
> Hey,


Hey. Post your question once, not thrice.

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Windows] Any way to distinguish ^C Induced EOF from ^Z EOF? Jan Burse Java 67 03-14-2012 12:21 AM
ifstream eof not reporting eof? SpreadTooThin C++ 10 06-15-2007 08:49 AM
if EOF = -1, can't a valid character == EOF and cause problems? Kobu C Programming 10 03-04-2005 10:40 PM
A question about EOF SL_McManus Perl 1 12-04-2003 01:50 AM
Newbie Question: EOF in MS Visual Studio 2003? entropy123 C Programming 6 07-03-2003 06:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57