By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,078 Members | 1,733 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,078 IT Pros & Developers. It's quick & easy.

Re: AFL (Australian Football League)

P: n/a
Bert said:
On Jul 11, 9:07 am, Bert <albert.xtheunkno...@gmail.comwrote:
<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
Jul 13 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Jul 13, 6:38 pm, Richard Heathfield <r...@see.sig.invalidwrote:
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.
Nah, programming comps don't need crap.
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.
-Little child to mummy-'I feel so special, mother'

[SIGH] - I found myself copying and pasting your name instead of
typing it out. You need to truncate your name.

FILE* in = fopen("aflin.txt" , "r");
if (in == NULL) { printf("Error"); return 0; }
FILE* out = fopen("alfout.txt", "w");
if (in == NULL) { printf("Error"); return 0; }

int nseats, t, b;

int richard_heathfield = fscanf(in, "%d", &nseats );
if (richard_heathfield != 1) { printf("Error"); return 0; }

richard_heathfield = fscanf(in, "%d", &t );
if (richard_heathfield != 1) { printf("Error"); return 0; }

int booked[t];
int i = 0;
for (; i < t; i++)
{
richard_heathfield = fscanf(in, "%d", &booked[i]);
if (richard_heathfield != 1) { printf("Error"); return 0; }
}

richard_heathfield = fscanf(in, "%d", &b);
if (richard_heathfield != 1) { printf("Error"); return 0; }
int bookings[nseats];

for (i = 0; i < b; i++)
{
richard_heathfield = fscanf(in, "%d", &bookings[i]);
if (richard_heathfield != 1) { printf("Error"); return 0; }
}
Jul 13 '08 #2

P: n/a
Bert said:
On Jul 13, 6:38 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>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.

Nah, programming comps don't need crap.
And yet you seem determined to supply it. Fair enough.

<snip>

--
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
Jul 13 '08 #3

P: n/a
Bert <al*****************@gmail.comwrites:
On Jul 13, 6:38 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>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.

Nah, programming comps don't need crap.
I want my program to check that my test input is OK even if the
customer never cares.
[SIGH] - I found myself copying and pasting your name instead of
typing it out. You need to truncate your name.
int nseats, t, b;

int richard_heathfield = fscanf(in, "%d", &nseats );
if (richard_heathfield != 1) { printf("Error"); return 0; }

richard_heathfield = fscanf(in, "%d", &t );
if (richard_heathfield != 1) { printf("Error"); return 0; }

int booked[t];
int i = 0;
for (; i < t; i++)
{
richard_heathfield = fscanf(in, "%d", &booked[i]);
if (richard_heathfield != 1) { printf("Error"); return 0; }
}

richard_heathfield = fscanf(in, "%d", &b);
if (richard_heathfield != 1) { printf("Error"); return 0; }
int bookings[nseats];

for (i = 0; i < b; i++)
{
richard_heathfield = fscanf(in, "%d", &bookings[i]);
if (richard_heathfield != 1) { printf("Error"); return 0; }
}
Good programmers never choose to repeat code like this. There is an
obvious candidate for a function here. In fact my solution to this
problem includes:

int read_num(FILE *fp)
{
static unsigned long line = 0;
int n;
line += 1;
char first[2];
if (fscanf(fp, "%1[0-9+-]", first) != 1 ||
ungetc(first[0], fp) == EOF ||
fscanf(fp, "%d\n", &n) != 1) {
fprintf(stderr, "Input error on line %lu.\n", line);
exit(EXIT_FAILURE);
}
return n;
}

I was prepared to discuss methods and compare code, but you seem to
have gone of the rails with sarcasm.

--
Ben.
Jul 14 '08 #4

P: n/a
Bert <al*****************@gmail.comwrites:
On Jul 13, 6:38 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>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.

Nah, programming comps don't need crap.
I want my program to check that my test input is OK even if the
customer never cares.
[SIGH] - I found myself copying and pasting your name instead of
typing it out. You need to truncate your name.
int nseats, t, b;

int richard_heathfield = fscanf(in, "%d", &nseats );
if (richard_heathfield != 1) { printf("Error"); return 0; }

richard_heathfield = fscanf(in, "%d", &t );
if (richard_heathfield != 1) { printf("Error"); return 0; }

int booked[t];
int i = 0;
for (; i < t; i++)
{
richard_heathfield = fscanf(in, "%d", &booked[i]);
if (richard_heathfield != 1) { printf("Error"); return 0; }
}

richard_heathfield = fscanf(in, "%d", &b);
if (richard_heathfield != 1) { printf("Error"); return 0; }
int bookings[nseats];

for (i = 0; i < b; i++)
{
richard_heathfield = fscanf(in, "%d", &bookings[i]);
if (richard_heathfield != 1) { printf("Error"); return 0; }
}
Good programmers never choose to repeat code like this. There is an
obvious candidate for a function here. In fact my solution to this
problem includes:

int read_num(FILE *fp)
{
static unsigned long line = 0;
int n;
line += 1;
char first[2];
if (fscanf(fp, "%1[0-9+-]", first) != 1 ||
ungetc(first[0], fp) == EOF ||
fscanf(fp, "%d\n", &n) != 1) {
fprintf(stderr, "Input error on line %lu.\n", line);
exit(EXIT_FAILURE);
}
return n;
}

I was prepared to discuss methods and compare code, but you seem to
have gone of the rails with sarcasm.

--
Ben.
Jul 14 '08 #5

P: n/a
Bert wrote:
>
.... snip ...
>
-Little child to mummy-'I feel so special, mother'

[SIGH] - I found myself copying and pasting your name instead of
typing it out. You need to truncate your name.

FILE* in = fopen("aflin.txt" , "r");
if (in == NULL) { printf("Error"); return 0; }
FILE* out = fopen("alfout.txt", "w");
if (in == NULL) { printf("Error"); return 0; }

int nseats, t, b;

int richard_heathfield = fscanf(in, "%d", &nseats );
if (richard_heathfield != 1) { printf("Error"); return 0; }
Congratulations. You have achieved my PLONK file. Bye.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Jul 14 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.