Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Do you see anything wrong with programme ?

Reply
Thread Tools

Do you see anything wrong with programme ?

 
 
Spiros Bousbouras
Guest
Posts: n/a
 
      05-15-2008
I got the following programme from an old post:
http://tinyurl.com/53oa6o

It was given at a job interview and the question was
"The following program works, but what is a
potential problem with it? "

#include <stdio.h>
#include <stdlib.h>

int main() {
char buf[128];
FILE *fp = fopen(__FILE__, "r");

if (!fp) return EXIT_FAILURE;
while(!feof(fp)) {
if (fgets(buf, sizeof buf, fp))
puts(buf);
}
fclose(fp);
return 0;
}

My opinion follows below but you might want
to think about it before scrolling down.





















The only problem I see with it is that it
might insert some extra newlines relative
to the original file. If the output of the
programme is meant to be recompiled
this might create problems. Otherwise
it seems fine to me.

Am I missing something ?
 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      05-15-2008
In article <(E-Mail Removed)>,
Spiros Bousbouras <(E-Mail Removed)> wrote:

>It was given at a job interview and the question was
>"The following program works, but what is a
>potential problem with it? "
>
>#include <stdio.h>
>#include <stdlib.h>
>
>int main() {
> char buf[128];
> FILE *fp = fopen(__FILE__, "r");
>
> if (!fp) return EXIT_FAILURE;
> while(!feof(fp)) {
> if (fgets(buf, sizeof buf, fp))
> puts(buf);
> }
> fclose(fp);
> return 0;
>}


>The only problem I see with it is that it
>might insert some extra newlines relative
>to the original file.


It certainly inserts a blank line between every line, and others if
any line exceeds the buffer size (which they don't if you're reading
this file, but see below). fgets() goes more naturally with fputs(),
and if you're not interpreting the text as lines it makes more sense
to use fread() and fwrite().


A more drastic problem is that __FILE__ is not guaranteed to refer
to the right file at run-time. If the source has been deleted, or
you're in a different directory, it's not going to work. It might
even refer to a completely different file.

-- Richard
--
:wq
 
Reply With Quote
 
 
 
 
David Resnick
Guest
Posts: n/a
 
      05-15-2008
On May 15, 10:46 am, Spiros Bousbouras <(E-Mail Removed)> wrote:
> I got the following programme from an old post:http://tinyurl.com/53oa6o
>
> It was given at a job interview and the question was
> "The following program works, but what is a
> potential problem with it? "
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main() {
> char buf[128];
> FILE *fp = fopen(__FILE__, "r");
>
> if (!fp) return EXIT_FAILURE;
> while(!feof(fp)) {
> if (fgets(buf, sizeof buf, fp))
> puts(buf);
> }
> fclose(fp);
> return 0;
>
> }
>
> My opinion follows below but you might want
> to think about it before scrolling down.
>
> The only problem I see with it is that it
> might insert some extra newlines relative
> to the original file. If the output of the
> programme is meant to be recompiled
> this might create problems. Otherwise
> it seems fine to me.
>
> Am I missing something ?


I don't do much file IO stuff, but fgets returns NULL on error. Is it
possible for it to return NULL but not set end of file? If so,
infinite loop...

And yes, puts will double the number of newlines. And lines longer
than
buf (not in that source, but could be) will be broken into pieces.
Seems
like printf is called for here...

-David
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      05-15-2008
In article <(E-Mail Removed)>,
David Resnick <(E-Mail Removed)> wrote:

>I don't do much file IO stuff, but fgets returns NULL on error. Is it
>possible for it to return NULL but not set end of file?


Yes, if it gets an i/o error instead.

>If so, infinite loop...


That depends on the nature of the error. I don't think the standard
says anything about whether future reads may succeed after a error,
or what they should read (e.g. should they retry to read the same
block of a file?).

-- Richard
--
:wq
 
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
Can you spot anything wrong with this class/structure? Jim Langston C++ 10 12-04-2007 04:39 PM
If you could add anything you want John Gagon Java 55 05-24-2006 11:16 PM
Can you see why I'm getting a NoClassDefFoundError here? I can't see it :< Flip Java 3 02-09-2004 10:13 PM



Advertisments