the EOF and characters in C 
Author Message
 the EOF and characters in C

<HTML>
Why is it that&nbsp; when the following code is used it doesn't work?
<BR>#include &lt;stdio.h>
<BR>main ()
<BR>{
<BR>&nbsp;&nbsp;&nbsp; char c;
<BR>&nbsp;&nbsp;&nbsp; FILE *fp;
<BR>&nbsp;&nbsp;&nbsp; fp=fopen("test.txt","r");
<BR>&nbsp;&nbsp;&nbsp; while ( (c=getc(fp)) != EOF)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putchar(c);
<BR>}

<P>this program keeps reading even after it encounters the EOF and spits
blank characters on the screen infinitely after the file is over.&nbsp;
The input file does exist and does have text material.&nbsp; In other words
it seems as if EOF is never encountered.

<P>Please help!!!!

<P>rr</HTML>



Wed, 05 Jan 2000 03:00:00 GMT  
 the EOF and characters in C



Quote:
>char c;
>while ( (c=getc(fp)) != EOF)
>this program keeps reading even after it encounters the EOF

Of course it does; getc() returns an int and you're trying
to stuff it into a char, which scrapes off bits that distinguish
-1 from 255 (for example); 255 != -1 so it never matches EOF.


Wed, 05 Jan 2000 03:00:00 GMT  
 the EOF and characters in C

Quote:

>Why is it that&nbsp; when the following code is used it doesn't work?
><BR>#include &lt;stdio.h>
><BR>main ()
><BR>{
><BR>&nbsp;&nbsp;&nbsp; char c;
><BR>&nbsp;&nbsp;&nbsp; FILE *fp;
><BR>&nbsp;&nbsp;&nbsp; fp=fopen("test.txt","r");
><BR>&nbsp;&nbsp;&nbsp; while ( (c=getc(fp)) != EOF)
><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putchar(c);
><BR>}

><P>this program keeps reading even after it encounters the EOF and spits
>blank characters on the screen infinitely after the file is over.&nbsp;
>The input file does exist and does have text material.&nbsp; In other words
>it seems as if EOF is never encountered.

><P>Please help!!!!

Please don't encode Usenet postings.  Your newsreader probably has
a setting to turn this bijou featurette off.

#include <stdio.h>
main ()
{
    /*
     *  'c' needs to be an int in order to be able to store all valid
     *  char values *and* EOF as well.
     *
     *  char c ;
     */
    int c;
    FILE *fp;
    fp=fopen("test.txt","r");
    while ( (c=getc(fp)) != EOF)
        putchar(c);
    /*
     *  And just for tidiness.
     */
    return 0;

Quote:
}

John
--
John Winters.  Wallingford, Oxon, England.


Wed, 05 Jan 2000 03:00:00 GMT  
 the EOF and characters in C

Quote:

><HTML>
>Why is it that&nbsp; when the following code is used it doesn't work?
><BR>#include &lt;stdio.h>
><BR>main ()
><BR>{
><BR>&nbsp;&nbsp;&nbsp; char c;

                        ^^^^
Have you tried "int c"?

Quote:
><BR>&nbsp;&nbsp;&nbsp; FILE *fp;
><BR>&nbsp;&nbsp;&nbsp; fp=fopen("test.txt","r");
><BR>&nbsp;&nbsp;&nbsp; while ( (c=getc(fp)) != EOF)

                                   ^^^^
How is getc() defined in stdio.h?

Quote:
><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putchar(c);
><BR>}

Shame about the html-stuff.

   Rozen verwelken,           \--------------------\
   Schepen vergaan.             \ Jan van den Broek  \

   Een paard moet je slaan.         \--------------------\



Wed, 05 Jan 2000 03:00:00 GMT  
 the EOF and characters in C

Quote:

> Why is it that  when the following code is used it doesn't work?
> #include <stdio.h>
> main ()
> {
>     char c;
>     FILE *fp;
>     fp=fopen("test.txt","r");
>     while ( (c=getc(fp)) != EOF)
>         putchar(c);
> }

> this program keeps reading even after it encounters the EOF and spits
> blank characters on the screen infinitely after the file is over.  The
> input file does exist and does have text material.  In other words it
> seems as if EOF is never encountered.

> Please help!!!!

 Hi,
   you can find an answer in the FAQ 12.1;
   the FAQ can be found at http://www.eskimo.com/~scs/C-faq
   Free hint : EOF is not of the type char.

        Regards,
                Alex Krol



Thu, 06 Jan 2000 03:00:00 GMT  
 the EOF and characters in C

Quote:

> Try this

> FILE *Fp ;
> char c ;
> Fp = fopen ("test.txt". "r") ;
> while ((C = getc (Fp)) !=EOF)
>    putc (c, stdout) ;
> }

This will generate an error since C is an undefined identifier.  Even if
your compiler gets past the C (which, I think, should be c), it will
{*filter*}on the unmatched closing brace.

Better to try this:

FILE *Fp;
int c;  /* getc() returns an int. */
Fp = fopen("test.txt". "r");
while((c = getc(Fp))!=EOF)
   putc( (char)c, stdout);

--
********************************************

********************************************
I've never had a humble opinion in my life.
If you're going to have one,
why bother to be humble about it?
                                Joan Baez



Thu, 06 Jan 2000 03:00:00 GMT  
 the EOF and characters in C

Try this

FILE *Fp ;
char c ;
Fp = fopen ("test.txt". "r") ;
while ((C = getc (Fp)) !=EOF)
   putc (c, stdout) ;

Quote:
}



Fri, 07 Jan 2000 03:00:00 GMT  
 the EOF and characters in C

# Try this

...if you want to see a linker error or what???

# FILE *Fp ;
# char c ;
# Fp = fopen ("test.txt". "r") ;
# while ((C = getc (Fp)) !=EOF)
#    putc (c, stdout) ;
# }


his homework assignment three times to this newsgroup. Please,
read a good book, preferably K&R2 and the FAQ before posting again.

Close to getting grumpy...

Regards,

        Jens
--
Jens Schweikhardt  http://www.uni-stuttgart.de/People/schweikhardt/home.html
SIGSIG -- signature too long (core dumped)



Fri, 07 Jan 2000 03:00:00 GMT  
 the EOF and characters in C



Quote:
>Better to try this:

>FILE *Fp;
>int c;  /* getc() returns an int. */
>Fp = fopen("test.txt". "r");
>while((c = getc(Fp))!=EOF)
>   putc( (char)c, stdout);

Why the cast? putc() takes an int too!

--
-----------------------------------------


-----------------------------------------



Fri, 07 Jan 2000 03:00:00 GMT  
 the EOF and characters in C

Quote:

>Try this

>FILE *Fp ;
>char c ;
>Fp = fopen ("test.txt". "r") ;
>while ((C = getc (Fp)) !=EOF)
>   putc (c, stdout) ;
>}

Do not do this.  getc(3) returns an int, not a char.

This is necessary to get EOF to be returned correctly.

--

``Not only is UNIX dead, it's starting to smell really bad.'' -- rob



Fri, 07 Jan 2000 03:00:00 GMT  
 the EOF and characters in C

On Sun, 20 Jul 1997 23:11:10 -0400, Alicia Carla Longstreet

Quote:

>Better to try this:

It still will not compile because of the typo.
Quote:

>FILE *Fp;
>int c;  /* getc() returns an int. */
>Fp = fopen("test.txt". "r");

                                ^^^
   if((Fp = fopen("test.txt","r")) == NULL)
         /* Code in case the attempt fails  */

Quote:
>while((c = getc(Fp))!=EOF)
>   putc( (char)c, stdout);

Al Bowers                                
Tampa, FL

http://www.gate.net/~abowers/index.html


Fri, 07 Jan 2000 03:00:00 GMT  
 the EOF and characters in C

Maybe you shouldn't post such broken snippets.

#include <stdio.h>

int main( void )
{
    FILE *Fp ;

    char c ;

    Fp = fopen ("test.txt". "r") ;  /* Line 9. */

    while ((C = getc (Fp)) !=EOF)   /* Line 11. */
        putc (c, stdout) ;

    return 0;    

Quote:
}

gcc testing.c

testing.c: In function `main':
testing.c:9: parse error before string constant
testing.c:11: `C' undeclared (first use this function)
testing.c:11: (Each undeclared identifier is reported only once
testing.c:11: for each function it appears in.)

                                                                azbok



Fri, 07 Jan 2000 03:00:00 GMT  
 the EOF and characters in C

Quote:

> Try this

> FILE *Fp ;
> char c ;
> Fp = fopen ("test.txt". "r") ;
> while ((C = getc (Fp)) !=EOF)
>    putc (c, stdout) ;


What did you try to achieve. You did not detect or correct the bug in
the original code. Instead you added 2 new bugs so that the code now
does not even compile:
  - function parameters (here "fopen()") are separated by ',' not '.'
  - it's 'c' not 'C'
Your replacement of "putchar()"  with "putc()" does *not* help.

The original code was:
 #include <stdio.h>
 main ()
 {
    char c;
    FILE *fp;
    fp=fopen("test.txt","r");
    while ( (c=getc(fp)) != EOF)
        putchar(c);
 }

The problem here is that "getc()" is a function with return type "int" !
This is very important in this context, because EOF does not fit into
a normal character variable (on *most* systems anyway). Casting the
"getc()" result to char (by assignment) will cripple the EOF. Comparing
the crippled EOF to the correct EOF will *not* be true.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Fri, 07 Jan 2000 03:00:00 GMT  
 the EOF and characters in C

Quote:

> Maybe you shouldn't post such broken snippets.

> #include <stdio.h>

> int main( void )
> {
>     FILE *Fp ;

>     char c ;

>     Fp = fopen ("test.txt". "r") ;  /* Line 9. */

make the . a ,  ------------^
Quote:

>     while ((C = getc (Fp)) !=EOF)   /* Line 11. */

--------------^
case is important change C to c
Quote:
>         putc (c, stdout) ;

>     return 0;
> }

> gcc testing.c

> testing.c: In function `main':
> testing.c:9: parse error before string constant
> testing.c:11: `C' undeclared (first use this function)
> testing.c:11: (Each undeclared identifier is reported only once
> testing.c:11: for each function it appears in.)

>                                                                 azbok



Sat, 08 Jan 2000 03:00:00 GMT  
 the EOF and characters in C

You should be using an int to receive input from fgetc and its macro
counterpart.

-Mark Lane

Quote:

> Why is it that  when the following code is used it doesn't work?
> #include <stdio.h>
> main ()
> {
>     char c;
>     FILE *fp;
>     fp=fopen("test.txt","r");
>     while ( (c=getc(fp)) != EOF)
>         putchar(c);
> }

> this program keeps reading even after it encounters the EOF and spits
> blank characters on the screen infinitely after the file is over.  The
> input file does exist and does have text material.  In other words it
> seems as if EOF is never encountered.

> Please help!!!!

> rr



Mon, 17 Jan 2000 03:00:00 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. EOF character in C

2. read() blocking on EOF character 0x1A

3. what is exactly EOF character...

4. Getting past EOF characters in text file.

5. How to insert an EOF character?

6. EOF character

7. Newbie: separate big .cs file into small .cs files

8. When Is EOF not EOF?

9. Find eof without using EOF

10. EOF or not EOF

11. multi-character character constant

12. Support for characters not in default character set?

 

 
Powered by phpBB® Forum Software