home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 192_01 / compfile.c < prev    next >
Text File  |  1979-12-31  |  2KB  |  100 lines

  1. /* DECLARE VARIABLES */
  2. char infile1[30];
  3. char infile2[30];
  4. int file1, file2;
  5. char a,b;
  6.   
  7. /* --- EXECUTABLE CODE STARTS HERE --- */
  8. main()
  9. {
  10.  char errflg,r;
  11.  int i, n = 0;
  12.  unsigned int lineno;
  13.  
  14. /* display header */
  15.  lineno = 1;
  16.  errflg = 0;
  17.  printf("File compare routine.  Version 1.0 by Joe Kilar \n");
  18.  printf("Any byte counts and values are shown in hex.\n \n");
  19.  
  20. /* ignore spacing? */
  21.  printf("Do you wish to ignore spacing? (Y/N) N is default:");
  22.  r = getchar();
  23.  if ((r=='y') || (r=='Y'))
  24.    r = 1;
  25.  else
  26.    r = 0;
  27.  putchar('\n');
  28.  
  29. /* prompt for and open files to be compared */
  30.  puts("Enter name of first file: ");
  31.  gets(infile1);
  32.  puts("Enter name of second file: ");
  33.  gets(infile2);
  34.  file1 = fopen(infile1,"r");
  35.  if (!file1)
  36.   {
  37.    printf("Unable to open file: %s \n",infile1);
  38.    exit();
  39.   }
  40.  file2 = fopen(infile2,"r");
  41.  if (!file2)
  42.   {
  43.    printf("Unable to open file: %s \n",infile2);
  44.    exit();
  45.   }
  46.  
  47. /* loop while compare continues */
  48.  while (errflg == 0)
  49.   {
  50. getnew1:
  51.    if ((i = fgetc(file1)) != -1)     /* if not end of file 1 */
  52.     {
  53.      a = i;
  54.      if (a == 13)       /* if carriage return, inc line no. count */
  55.        lineno++;
  56.      n++;               /* increment byte count */
  57.      if (r && isspace(a))
  58.        goto getnew1;
  59. getnew2:
  60.      if ((i = fgetc(file2)) != -1)     /* if not end of file 2 */
  61.       {
  62.        b = i;
  63.        if (r && isspace(b))
  64.          goto getnew2;
  65.        if (a != b)       /* if bytes don't compare */
  66.         {
  67.          printf("Files fail compare at byte number: %x \n",n);
  68.          printf("First file's byte is: %x  \n",a);
  69.          printf("Second file's byte is: %x \n",b); 
  70.          printf("If ASCII files, failure is at line %d (decimal).\n",lineno);
  71.          exit();
  72.         }
  73.       }
  74.      else    /* end of file 2, so first is longer */
  75.       {
  76.        printf("First file is longer. \n");
  77.        exit();
  78.       }
  79.     }
  80.    else
  81.     {
  82.      if ((b = fgetc(file2)) != 0xff)
  83.       {
  84.        printf("Second file is longer. \n");
  85.        exit();
  86.       }
  87.      else
  88.       {
  89.        printf("Files compare OK. \n");
  90.        printf("%x number of bytes compared OK. \n"); 
  91.        exit();
  92.       }
  93.     }
  94.   }
  95. }
  96.  
  97.  
  98.  
  99. 
  100.