/* Program to demonstrate simple error reporting */

#include <stdio.h>  /* for fopen, fprintf */
#include <stdlib.h> /* for exit */
#include <string.h> /* for strerror */
#include <errno.h>  /* for errno */

int
main(int argc, char* argv[])
{

  if (argc != 2)                            /* Verify user command-line input */
    {
      fprintf(stderr, "Usage: %s filename\n",argv[0]);
      exit(EXIT_FAILURE);
    }
  
  FILE* stream = fopen(argv[1],"r");        /* Open the file named by the user */

  if (stream == NULL)                       /* Verify the open was successful */
    {                                       /* Report a failure */
      fprintf(stderr, "%s: Cannot open %s: %s\n",
              argv[0], argv[1], strerror(errno));
      exit(EXIT_FAILURE);
    }

  /* stream ready for reading with fread, fgets, etc. ... */

  if (fclose(stream))                       /* Close the file stream */
    {
      fprintf(stderr, "%s: Error closing file %s: %s\n",
              argv[0], argv[1], strerror(errno));
      exit(EXIT_FAILURE);
    }

    return 0; // actually EXIT_SUCCESS
}
