Do you see anything wrong with programme ?

S

Spiros Bousbouras

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 ?
 
R

Richard Tobin

Spiros Bousbouras said:
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
 
D

David Resnick

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
 
R

Richard Tobin

David Resnick said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

error 28
Anything wrong with this code? 3
getline problem 10
code 34
write error 13
Anything to worry about (compiler warnings)? 14
Linux programme - different results each run 5
Help with C negative indexes 2

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top