Sunday, February 1, 2015

Read file perform operation and output file in C


#include <stdio.h>
void main()
{
  int i,j;
  float A[3][3];
  float B[3][3];
  FILE *inputMatrix=fopen("filein.txt","r");
  for(i=0;i<3;++i){
     for(j=0;j<3;++j){
         fscanf(inputMatrix,"%f",&A[i][j]);
     }
  }
  fclose(inputMatrix);

//Operation
 for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            B[i][j] = A[i][j] + A[i][j];
        }
    }

//write
   FILE *f;
   f = fopen("output.txt","w");
   for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
        fprintf(f,"\t %f",B[i][j]); 
        }
                fprintf(f,"\n"); 
       }
   fclose(f);
}

No comments: