When I have several files, I want to create a program that checks if there are any files in the same file. To do this, create a program that checks whether the contents of the two files are the same.

The file names of the two files to be examined are given from the command line (command line). If the contents are the same, the names of the two files are separated by "," and output as ": IDENTICAL", otherwise ": DIFFERENT" is output.

First, for the two given file names, the names are written in the order given, separated by ",". At this time, confirm that the file can be opened. When it is found that either of them cannot be opened, ": can't open." Is written immediately after the file name at that point, and then execution is terminated after returning a line feed (return 0;). As a result, when the file cannot be opened with the first file name, ": can't open." Follows immediately after the first file name, and when the file cannot be opened with the second file name. Two file names separated by "," are arranged, and ": can't open." Is arranged immediately after the second file name

■ Success example
Command line input
data1.txt data2.txt

Standard output
data1.txt, data2.txt: DIFFERENT

Command line input
data3.txt data1.txt

Standard output
data3.txt, data1.txt: IDETICAL

Command line input
data4.txt data1.txt

Standard output
data4.txt: can't open.

Command line input
data3.txt data1.c

Standard output
data3.txt, data1.c: can't open.

# include<stdio.h>
int main (int argc, char * argv []) {
  #define fn "data1.txt"
  #define gn "data2.txt"
  #define kn "data3.txt"
  FILE * f, * g, * k;
  f = fopen (fn, "r");g = fopen (gn, "r");k = fopen (kn, "r");
  if (f == NULL) {
    printf ("NG:% s \ n", fn);exit (-1);
  g = fopen (gn, "r");
  if (g == NULL) {
    printf ("NG:% s \ n", gn);exit (-1);
  if (k == NULL) {
    printf ("NG:% s \ n", kn);exit (-1);
  int cf, cg, ck;
  while ((cf = fgetc (f))! = EOF&&
         (cg = fgetc (g))! = EOF&&
         (ck = fgetc (k))! = EOF) {
    if (cf! = cg) break;
  if (! (cf == EOF&&fgetc (g) == EOF))
    printf ("% s,% s: DIFFERENT \ n", fn, gn);
  fclose (f);fclose (g);
    return 0;

More information
I would like to know the correct program. . .

  • Answer # 1

    From the previous questions, it seems that the questioner is practicing Terao-driven development, so only the code is given.

    There was nothing to mention when a read error occurred, so we decided to add "error occured."

    # include
    int main (int argc, char * argv [])
        if (argc! = 3) return 1;
        FILE * f1 = fopen (argv [1], "rb");
        if (f1 == NULL) {
            printf ("% s: can't open. \ n", argv [1]);
            return 0;
        FILE * f2 = fopen (argv [2], "rb");
        if (f2 == NULL) {
            printf ("% s,% s: can't open. \ n", argv [1], argv [2]);
            return 0;
        char * message = "IDETICAL";
        do {
            if (fgetc (f1)! = fgetc (f2)) {
                message = "DIFFERENT";
            if (ferror (f1) || ferror (f2)) {
                message = "error occured.";
        } while (! feof (f1));
        printf ("% s,% s:% s \ n", argv [1], argv [2], message);
        return 0;