IHYPRESS PROGRAMMING
Tutorials and C programs with code and output for beginners
c programming
HOME | ASP | C | CSS | GNUPLOT | HTML | JAVASCRIPT | PERL | PHP | PYTHON | RUBY | SVG
CLoops : End-of-File Loop without Status
<04.09>
/* Birdwatching program (EOF loop, no status var) */
#include <stdio.h> int main (void) { FILE *birdfile; int birds, days, total, i, j; birdfile = fopen ("birds.txt", "r"); total = 0; /* loop goes while there is data */ while (fscanf (birdfile, "%d", &days) != EOF) { /* loop varies with no of days of sightings */ for (j=1; j<=days; ++j) { fscanf (birdfile, "%d", &birds); total = total + birds; } } fclose (birdfile); printf ("Total number of birds sighted: %d\n", total); return(0); }
Hergestellt in Deutschland / Made in Germany
Total number of birds sighted: 219
birds.txt file contains:
3 4 6 7
3 1 1 1
1 1
0
7 1 5 9 11 5 4 1
2 9 1
1 3
8 2 4 6 9 12 23 4 12
6 4 6 3 9 4 11
3 2 3 7
1 9
2 9 10
COPYRIGHT © 2015-2024 IHY PRESS Frankfurt am Main 60329 Deutschland