Re: AFL (Australian Football League)



Bert said:

On Jul 11, 9:07 am, Bert <albert.xtheunkno...@xxxxxxxxx> wrote:
<snip>
FILE* in = fopen("aflin.txt" , "r");
<snip>

'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
.



Relevant Pages

  • Re: pointer-in-array test
    ... >> system where it will fail to give the correct answer, ... The result of the conversion to `long' is implementation-defined, ... If `long' cannot represent the pointer value, ... C does not have implicit type casts. ...
    (comp.lang.c)
  • Re: set and reset the least significant bit of a address
    ... > The conversion to and from intptr_t is implementation defined. ... property that any valid pointer to void can be converted to ... The current C standard is C99. ... What I fail to understand are the people that always ...
    (comp.lang.c)
  • Re: slurping in binary data
    ... I think it's poor design to write code that fails catastrophically when given incorrect inputs. ... That means that they fail without undefined behavior, and with an informative error message, if possible. ... It's a lot harder to achieve that goal with fscanf() than it is with fgets/sscanf. ...
    (comp.lang.c)
  • Re: How to read the file?
    ... Is it possible using "fscanf" to read this kind of file? ... I fail to read through the part of the data ... You could also use textread (or textscan): ...
    (comp.soft-sys.matlab)
  • Re: How to read the file?
    ... I try to use fscanf to read it, but it fail. ... You could also use textread (or textscan): ... Titus ...
    (comp.soft-sys.matlab)