/* a program that just demonstrates how to write output to a file */

#include <stdio.h>

int main ()
{
  FILE *outfile;  /* declare a file pointer */
  int x=3;        /* x and y are variables just for demonstration purposes */
  double y=4.5;
  
  /* open the output file */
  
  outfile = fopen("writeToFile.dat","w"); /* try to open file for writing */
  if (outfile == NULL)               /* check return status */
    {
      perror("File writeToFile.dat could not be opened: "); /* print error */
      exit(-1);
    }
  
  /* write some output to the file */
  
  fprintf(outfile,"This output is going to the file writeToFile.dat.\n");
  
  printf("This output is going to the terminal.\n");
  printf("I can still write to the terminal in the same program\n");
  printf("that I am writing to a file.\n");
  
  fprintf(outfile,"I can also print the value of variables to a file.\n");
  fprintf(outfile,"for example, x=%d, y=%.2f\n",x,y);
  fprintf(outfile,"Bye now!\n");
  
  /* close the output file */
  
  fclose(outfile);
  
  return 0;
}



