Practice Exercise #3

Write a C program that does a whitespace insensitive comparison of contents of two files. The program exits with code 0 if the files are identical, except possibly for differences in whitespace. It exits with code 1 if the files are different, ignoring whitespace. It exits with code 2 if any kind of error occurs. It never prints to stdout.

Details:

  • You might want to use fopen, fgetc, fclose, perror, isspace, fprintf, and stderr.
  • If there are no errors, your program shouldn't print anything. It should simply exit with either a 0 (match) or 1 (don't match) exit code. In this circumstance, you should close all open files before terminating, and valgrind should give a totally clean report.
  • If there are errors, your program should print a message to stderr and exit with exit code 2. In this circumstance, you don't need to close open files before terminating, and the valgrind report might not be totally clean.
  • Your code should be invocable like this (this synatx might require bash):
    bash$ ./ex3 ex3.c ex3.c
    bash$ echo $?
    0
    bash$ if ./ex3 ex3.c ex3.c; then echo Match; else echo No match; fi
    Match
    bash$ if ./ex3 ex3.c ex3Temp.c; then echo Match; else echo No match; fi
    No match
    bash$ if ./ex3 ex3.c noSuchFile; then echo Match; else echo No match; fi
    noSuchFile: No such file or directory
    No match
    bash$ ./ex3
    Usage: ./ex3 file file
    Compares two files in a whitespace insensitive way.
    
    We care about the exit codes (0, 1, or 2), but not about the details of the error messages your code might print.