Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > a problem when using int feof(FILE *fp);

Reply
Thread Tools

a problem when using int feof(FILE *fp);

 
 
Chad
Guest
Posts: n/a
 
      07-13-2008
On Jul 13, 7:14 am, santosh <(E-Mail Removed)> wrote:
> Chad wrote:
> > On Jul 13, 6:48 am, santosh <(E-Mail Removed)> wrote:
> >> Chad wrote:
> >> > On Jul 12, 10:47 pm, Barry Schwarz <(E-Mail Removed)> wrote:
> >> >> On Sat, 12 Jul 2008 22:36:38 -0700 (PDT), Chad <(E-Mail Removed)>
> >> >> wrote:
> >> >> >On Jul 12, 4:24 am, (E-Mail Removed) (Jens Thoms Toerring) wrote:
> >> >> >> #include <stdio.h>
> >> >> >> #include <stdlib.h>

>
> >> >> >> int main( void )
> >> >> >> {
> >> >> >> FILE *fp;
> >> >> >> int c1,
> >> >> >> char c2;

>
> >> >> >> if ( ( fp = fopen( "test.txt", "r" ) == NULL )
> >> >> >> {
> >> >> >> fprintf( stderr, "Failed to open file\n" );
> >> >> >> return EXIT_FAILURE;
> >> >> >> }

>
> >> >> >> /* Read until nothing can be read anymore */

>
> >> >> >> while ( ( c1 = fgetc( fp ) ) != EOF )
> >> >> >> putchar( c1 );

>
> >> >> >> #if 0
> >> >> >> /* or, when using fscanf() */

>
> >> >> >> while ( fscanf( fp, "%c", &c2 ) == 1 )
> >> >> >> putc( c2 )

>
> >> >> >> /* or, when using fread() */

>
> >> >> >> while ( fread( &c2, 1, 1, fp ) == 1 )
> >> >> >> putc( c2 )
> >> >> >> #endif

>
> >> >> >> /* Check why the last read failed */

>
> >> >> >> if ( feof( fp ) )
> >> >> >> fprintf( stderr, "End of file detected\n" );
> >> >> >> else
> >> >> >> fprintf( stderr, "Read error\n" );

>
> >> >> >> #if 0
> >> >> >> /* or using ferror() */

>
> >> >> >> if ( ferror( fp ) )
> >> >> >> fprintf( stderr, "Read error\n" );
> >> >> >> else
> >> >> >> fprintf( stderr, "End of file detected\n" );
> >> >> >> #endif

>
> >> >> >> close( fp );
> >> >> >> return 0;}

>
> >> >> >Can you, or someone else tell me why you are using #if 0 / #endif
> >> >> >vs just something like if/else,

>
> >> >> The #if 0 suppresses the compilation of that block of code. It's
> >> >> as if the code were not present in the source module.

>
> >> >> An if (0) is only required to suppress the execution of the block.
> >> >> An optimizing compiler can still optimize it away but there is no
> >> >> requirement that it do so.
> >> > Is there a particular advantage to using #if 0 (in this case)? Does
> >> > suppressing the compilation of that block code just make the
> >> > program run faster? Or is there something more?

>
> >> In this particular case the first set of #if 0 ... #endif is needed,
> >> otherwise functionality is duplicated. The second pair isn't strictly
> >> needed.

>
> >> In general blocks of code are commented out (with any method) because
> >> they are either buggy or deemed not necessary or fit for some reason.
> >> It has nothing to do with runtime speed.

>
> > I don't see how the using something like if# 0 .. #endif removes
> > duplicate functionality in this case. Okay, it just hurts to think
> > right about now. Maybe in a few hours everything will sink in.

>
> In the code that Jens has presented he use fgetc in a loop to read from
> a file and output characters until EOF is encountered. The code in the
> first pair of #if 0... #endif are simply alternate forms of the same
> read loop using instead of fgetc, fscanf and fread. If they had been
> compiled (i.e., if the #if 0... #endif were absent), they too would be
> executed after the first loop has run it's course. They would most
> probably immediately return end-of-file since after the first loop fp
> is already read.
>
> This is a case where the #if 0 and #endif are not critical to
> correctness, but generally code that has been preprocessed or commented
> out should not be compiled without a careful review.



Okay, I think this is the part that is not *clicking*. I don't see
when the code would skip over fgetc() and use something like fread()
instead.

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      07-13-2008
Chad wrote:

> On Jul 13, 7:14 am, santosh <(E-Mail Removed)> wrote:
>> Chad wrote:
>> > On Jul 13, 6:48 am, santosh <(E-Mail Removed)> wrote:
>> >> Chad wrote:
>> >> > On Jul 12, 10:47 pm, Barry Schwarz <(E-Mail Removed)> wrote:
>> >> >> On Sat, 12 Jul 2008 22:36:38 -0700 (PDT), Chad
>> >> >> <(E-Mail Removed)> wrote:
>> >> >> >On Jul 12, 4:24 am, (E-Mail Removed) (Jens Thoms Toerring)
>> >> >> >wrote:
>> >> >> >> #include <stdio.h>
>> >> >> >> #include <stdlib.h>

>>
>> >> >> >> int main( void )
>> >> >> >> {
>> >> >> >> FILE *fp;
>> >> >> >> int c1,
>> >> >> >> char c2;

>>
>> >> >> >> if ( ( fp = fopen( "test.txt", "r" ) == NULL )
>> >> >> >> {
>> >> >> >> fprintf( stderr, "Failed to open file\n" );
>> >> >> >> return EXIT_FAILURE;
>> >> >> >> }

>>
>> >> >> >> /* Read until nothing can be read anymore */

>>
>> >> >> >> while ( ( c1 = fgetc( fp ) ) != EOF )
>> >> >> >> putchar( c1 );

>>
>> >> >> >> #if 0
>> >> >> >> /* or, when using fscanf() */

>>
>> >> >> >> while ( fscanf( fp, "%c", &c2 ) == 1 )
>> >> >> >> putc( c2 )

>>
>> >> >> >> /* or, when using fread() */

>>
>> >> >> >> while ( fread( &c2, 1, 1, fp ) == 1 )
>> >> >> >> putc( c2 )
>> >> >> >> #endif

>>
>> >> >> >> /* Check why the last read failed */

>>
>> >> >> >> if ( feof( fp ) )
>> >> >> >> fprintf( stderr, "End of file detected\n" );
>> >> >> >> else
>> >> >> >> fprintf( stderr, "Read error\n" );

>>
>> >> >> >> #if 0
>> >> >> >> /* or using ferror() */

>>
>> >> >> >> if ( ferror( fp ) )
>> >> >> >> fprintf( stderr, "Read error\n" );
>> >> >> >> else
>> >> >> >> fprintf( stderr, "End of file detected\n" );
>> >> >> >> #endif

>>
>> >> >> >> close( fp );
>> >> >> >> return 0;}

>>
>> >> >> >Can you, or someone else tell me why you are using #if 0 /
>> >> >> >#endif vs just something like if/else,

>>
>> >> >> The #if 0 suppresses the compilation of that block of code.
>> >> >> It's as if the code were not present in the source module.

>>
>> >> >> An if (0) is only required to suppress the execution of the
>> >> >> block. An optimizing compiler can still optimize it away but
>> >> >> there is no requirement that it do so.
>> >> > Is there a particular advantage to using #if 0 (in this case)?
>> >> > Does suppressing the compilation of that block code just make
>> >> > the program run faster? Or is there something more?

>>
>> >> In this particular case the first set of #if 0 ... #endif is
>> >> needed, otherwise functionality is duplicated. The second pair
>> >> isn't strictly needed.

>>
>> >> In general blocks of code are commented out (with any method)
>> >> because they are either buggy or deemed not necessary or fit for
>> >> some reason. It has nothing to do with runtime speed.

>>
>> > I don't see how the using something like if# 0 .. #endif removes
>> > duplicate functionality in this case. Okay, it just hurts to think
>> > right about now. Maybe in a few hours everything will sink in.

>>
>> In the code that Jens has presented he use fgetc in a loop to read
>> from a file and output characters until EOF is encountered. The code
>> in the first pair of #if 0... #endif are simply alternate forms of
>> the same read loop using instead of fgetc, fscanf and fread. If they
>> had been compiled (i.e., if the #if 0... #endif were absent), they
>> too would be executed after the first loop has run it's course. They
>> would most probably immediately return end-of-file since after the
>> first loop fp is already read.
>>
>> This is a case where the #if 0 and #endif are not critical to
>> correctness, but generally code that has been preprocessed or
>> commented out should not be compiled without a careful review.

>
>
> Okay, I think this is the part that is not *clicking*. I don't see
> when the code would skip over fgetc() and use something like fread()
> instead.


It won't "skip over" anything. If you want to use fscanf, you block out
the loops using fgetc and fread. If you want to use fread, you block
out the code using fgetc and fscanf. If you want to use fgetc, you
block out code that uses fscanf and fread, as in the version posted by
Jens. Simple, isn't it?

 
Reply With Quote
 
 
 
 
Chad
Guest
Posts: n/a
 
      07-13-2008
On Jul 13, 7:29 am, santosh <(E-Mail Removed)> wrote:
> Chad wrote:
> > On Jul 13, 7:14 am, santosh <(E-Mail Removed)> wrote:
> >> Chad wrote:
> >> > On Jul 13, 6:48 am, santosh <(E-Mail Removed)> wrote:
> >> >> Chad wrote:
> >> >> > On Jul 12, 10:47 pm, Barry Schwarz <(E-Mail Removed)> wrote:
> >> >> >> On Sat, 12 Jul 2008 22:36:38 -0700 (PDT), Chad
> >> >> >> <(E-Mail Removed)> wrote:
> >> >> >> >On Jul 12, 4:24 am, (E-Mail Removed) (Jens Thoms Toerring)
> >> >> >> >wrote:
> >> >> >> >> #include <stdio.h>
> >> >> >> >> #include <stdlib.h>

>
> >> >> >> >> int main( void )
> >> >> >> >> {
> >> >> >> >> FILE *fp;
> >> >> >> >> int c1,
> >> >> >> >> char c2;

>
> >> >> >> >> if ( ( fp = fopen( "test.txt", "r" ) == NULL )
> >> >> >> >> {
> >> >> >> >> fprintf( stderr, "Failed to open file\n" );
> >> >> >> >> return EXIT_FAILURE;
> >> >> >> >> }

>
> >> >> >> >> /* Read until nothing can be read anymore */

>
> >> >> >> >> while ( ( c1 = fgetc( fp ) ) != EOF )
> >> >> >> >> putchar( c1 );

>
> >> >> >> >> #if 0
> >> >> >> >> /* or, when using fscanf() */

>
> >> >> >> >> while ( fscanf( fp, "%c", &c2 ) == 1 )
> >> >> >> >> putc( c2 )

>
> >> >> >> >> /* or, when using fread() */

>
> >> >> >> >> while ( fread( &c2, 1, 1, fp ) == 1 )
> >> >> >> >> putc( c2 )
> >> >> >> >> #endif

>
> >> >> >> >> /* Check why the last read failed */

>
> >> >> >> >> if ( feof( fp ) )
> >> >> >> >> fprintf( stderr, "End of file detected\n" );
> >> >> >> >> else
> >> >> >> >> fprintf( stderr, "Read error\n" );

>
> >> >> >> >> #if 0
> >> >> >> >> /* or using ferror() */

>
> >> >> >> >> if ( ferror( fp ) )
> >> >> >> >> fprintf( stderr, "Read error\n" );
> >> >> >> >> else
> >> >> >> >> fprintf( stderr, "End of file detected\n" );
> >> >> >> >> #endif

>
> >> >> >> >> close( fp );
> >> >> >> >> return 0;}

>
> >> >> >> >Can you, or someone else tell me why you are using #if 0 /
> >> >> >> >#endif vs just something like if/else,

>
> >> >> >> The #if 0 suppresses the compilation of that block of code.
> >> >> >> It's as if the code were not present in the source module.

>
> >> >> >> An if (0) is only required to suppress the execution of the
> >> >> >> block. An optimizing compiler can still optimize it away but
> >> >> >> there is no requirement that it do so.
> >> >> > Is there a particular advantage to using #if 0 (in this case)?
> >> >> > Does suppressing the compilation of that block code just make
> >> >> > the program run faster? Or is there something more?

>
> >> >> In this particular case the first set of #if 0 ... #endif is
> >> >> needed, otherwise functionality is duplicated. The second pair
> >> >> isn't strictly needed.

>
> >> >> In general blocks of code are commented out (with any method)
> >> >> because they are either buggy or deemed not necessary or fit for
> >> >> some reason. It has nothing to do with runtime speed.

>
> >> > I don't see how the using something like if# 0 .. #endif removes
> >> > duplicate functionality in this case. Okay, it just hurts to think
> >> > right about now. Maybe in a few hours everything will sink in.

>
> >> In the code that Jens has presented he use fgetc in a loop to read
> >> from a file and output characters until EOF is encountered. The code
> >> in the first pair of #if 0... #endif are simply alternate forms of
> >> the same read loop using instead of fgetc, fscanf and fread. If they
> >> had been compiled (i.e., if the #if 0... #endif were absent), they
> >> too would be executed after the first loop has run it's course. They
> >> would most probably immediately return end-of-file since after the
> >> first loop fp is already read.

>
> >> This is a case where the #if 0 and #endif are not critical to
> >> correctness, but generally code that has been preprocessed or
> >> commented out should not be compiled without a careful review.

>
> > Okay, I think this is the part that is not *clicking*. I don't see
> > when the code would skip over fgetc() and use something like fread()
> > instead.

>
> It won't "skip over" anything. If you want to use fscanf, you block out
> the loops using fgetc and fread. If you want to use fread, you block
> out the code using fgetc and fscanf. If you want to use fgetc, you
> block out code that uses fscanf and fread, as in the version posted by
> Jens. Simple, isn't it?



Yes.
 
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
why is int a[0] not allowed, but int* a = new int[0] is? haijin.biz@gmail.com C++ 9 04-17-2007 09:01 AM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int a[10]; int* p=(int*)((&a)+1); But why p isn't equal to ((&a)+1)? aling C++ 8 10-20-2005 02:42 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments