Re: AFL (Australian Football League)
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Sun, 13 Jul 2008 08:38:12 +0000
Bert said:
On Jul 11, 9:07 am, Bert <albert.xtheunkno...@xxxxxxxxx> wrote:<snip>
<snip>FILE* in = fopen("aflin.txt" , "r");
'You fail to check whether this call succeeded - fopen returns a null
pointer if it cannot open the file. You need to decide what you should
do in that circumstance, and then add code to carry out that plan if the
file fails to open. For example, you might prompt the user to give you a
different filename, or you might simply print an error message and
quit. But you can't just *assume* that the file will be opened
successfully, not if your goal is to become a good programmer. '
FILE* in = fopen("aflin.txt" , "r");
if (in == NULL) { printf("Error"); return 0; }
Just don't go crazy over its neatness.
Your next line is:
FILE* out = fopen("alfout.txt", "w");
You fail to check whether this call succeeded - fopen returns a null
pointer if it cannot open the file. You need to decide what you should
do in that circumstance, and then add code to carry out that plan if the
file fails to open. For example, you might prompt the user to give you a
different filename, or you might simply print an error message and
quit. But you can't just *assume* that the file will be opened
successfully, not if your goal is to become a good programmer.
Moving on... you read from your input file as follows:
fscanf(in, "%d", &nseats );
but you don't check whether this call succeeded. The fscanf function yields
a result that is either EOF (if an input error occurs before any
conversion) or 0 (if, say, the input doesn't make sense to fscanf in the
light of the format specifier, e.g. SIX instead of 6), or the number of
fields successfully converted. In your case, you want one field to be
converted, so you want fscanf to return 1. Your code should check that it
does, and you should decide what to do in the event that it doesn't. Many
learners decide simply to abort the program at this point. That's
acceptable while you're learning, but a more robust response would involve
explaining the problem to the user and offering another chance to provide
good input.
You have just provided some evidence that you are prepared to respond to
advice, which is why I've provided two tips this time rather than just
one. If you fix up your code again, next time I'll give you three. If you
don't want all three of them to be "check the result of /this/ fscanf
call", you can avoid that by adding code to check the results of all of
the fscanf calls, not just the one I mentioned above, before posting again
- this will save you a fair bit of time.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.
- Follow-Ups:
- Re: AFL (Australian Football League)
- From: Bert
- Re: AFL (Australian Football League)
- References:
- AFL (Australian Football League)
- From: Bert
- Re: AFL (Australian Football League)
- From: Bert
- AFL (Australian Football League)
- Prev by Date: Re: Commandline compiler for windows?
- Next by Date: Re: Commandline compiler for windows?
- Previous by thread: Re: AFL (Australian Football League)
- Next by thread: Re: AFL (Australian Football League)
- Index(es):
Relevant Pages
|