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
COverview of C : Input and Output Files
<01.08>
/* Working with multiple files */
/* (This program contains a few syntax errors. Try to correct them) */
#include <stdio.h> #define PI 3.1416 int main (void) { int a, b, c, d, e, total; FILE *f1, *f2, *f3; /* opening input files */ f1 = fopen ("file1.dat", "r"); f2 = fopen ("file2.dat", "r"); /* reading data */ fscanf (f1, "%d%d%d", &a, &b, &c); fscanf (f2, "%d%d%, &d, &e); /* adding the numbers */ total = a + b + c + d + e; /* closing input files */ fclose (f1); fclose (f2); /* opening output file */ f3 = fopen ("total.dat", "w"); /* output report on both screen and file */ printf ("Read from first file: %d %d %d\n", a, b, c); printf ("Read from second file: %d %d\n", d, e); printf ("The sum is: %d\n\n", total); fprintf ("f3, "%d", total); printf ("\nFILE total.dat CREATED.\n"); /* check your directory for the new file! */ /* closing output file */ fclose (f3); return (0); }
Hergestellt in Deutschland / Made in Germany
Read from first file: 34 78 122 Read from second file: 45 90 The sum is : 369 FILE total.dat CREATED.
COPYRIGHT © 2015-2024 IHY PRESS Frankfurt am Main 60329 Deutschland