Simplifying file outputs for printfs


Now that you know a little about strings and all about functions, you might 
consider using this knowledge to make life simpler in printing to a file.

Currently, whenever you printf to the terminal, you also fprintf to a file.  
Its mighty tedious and boring and you are likely to make errors.

Consider the following function which prints the string to both the terminal 
and also to a file.

     void wr(char *str)
     {
        printf("%s", str);
        fprintf(f1, "%s", str);
     }

Now, in your program, whenever you want to print, you can do something like 
this;

     main()
     {
        char s1[80]
        int a=2; b=3; c;
        c=a+b;
        sprintf(s1, "The value of a is %d\n", a);
        wr(s1);
        sprintf(s1, "The value of b is %d\n", b);
        wr(s1);
        sprintf(s1, "The value of c is %d\n", c);
        wr(s1);
     }

Note that in each case, you are sprintf ing to a string and then calling your 
wr to both printf and fprintf.

******

Now taking the MatPrint function from Assignment #6;

     void MatPrint(double x[][MATC])
     {
        int i, j;
        char s1[80];
        for (i=0; i<MATR;i++)
        {
           for(j=0; j<MATC; j++)
           {
              sprintf(s1, "lf ", x[i][j]);
              wr(str)
           }
           sprintf(s1, "\n");
           wr(s1);
        }
     }

There is also a way to similarly handle scanfs.

Simplifying File Output for scanfs