Skip to content

Instantly share code, notes, and snippets.

@samwightt
Created November 8, 2018 23:18
Show Gist options
  • Save samwightt/6aa9f63e4d7270e407d148b6f389b909 to your computer and use it in GitHub Desktop.
Save samwightt/6aa9f63e4d7270e407d148b6f389b909 to your computer and use it in GitHub Desktop.
Script to compare two pgm files to see if they are the same. Compile using: gcc -Wall compare.c. Run ./a.out to see usage.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
#define KNRM "\x1B[0m"
int main(int argv, char **argc) {
if (argv != 3) {
printf("This program compares two pgm files to see if they are the same. Whitespace is ignored.\n");
printf("Use: ./a.out <fileone.pgm> <filetwo.pgm>\n");
return 0;
}
FILE *fileOne;
FILE *fileTwo;
fileOne = fopen(argc[1], "r");
if (fileOne == NULL) {
printf("Could not open file one. Check to see if file exists.\n");
return 0;
}
fileTwo = fopen(argc[2], "r");
if (fileTwo == NULL) {
printf("Could not open file two. Check to see if file exists.\n");
return 0;
}
printf("Tests:\n");
char magic[2][3];
fscanf(fileOne, "%s", magic[0]);
fscanf(fileTwo, "%s", magic[1]);
printf("%s1. File encoding matches: ", KNRM);
if (strcmp(magic[0], magic[1]) != 0) {
printf("%sFAILED.\n", KRED);
printf("%s %s's content: %s\n", KNRM, argc[1], magic[0]);
printf("%s %s's content: %s\n", KNRM, argc[2], magic[1]);
}
else printf("%sPASSED.\n", KGRN);
int intHolder[2];
fscanf(fileOne, "%d", &intHolder[0]);
fscanf(fileTwo, "%d", &intHolder[1]);
printf("%s2. Width matches: ", KNRM);
if (intHolder[0] != intHolder[1]) {
printf("%sFAILED.\n", KRED);
printf("%s %s's content: %d\n", KNRM, argc[1], intHolder[0]);
printf("%s %s's content: %d\n", KNRM, argc[2], intHolder[1]);
}
else printf("%sPASSED.\n", KGRN);
fscanf(fileOne, "%d", &intHolder[0]);
fscanf(fileTwo, "%d", &intHolder[1]);
printf("%s3. Height matches: ", KNRM);
if(intHolder[0] != intHolder[1]) {
printf("%sFAILED.\n", KRED);
printf("%s %s's content: %d\n", KNRM, argc[1], intHolder[0]);
printf("%s %s's content: %d\n", KNRM, argc[2], intHolder[1]);
}
else printf("%sPASSED.\n", KGRN);
fscanf(fileOne, "%d", &intHolder[0]);
fscanf(fileTwo, "%d", &intHolder[1]);
printf("%s4. Intensity matches: ", KNRM);
if (intHolder[0] != intHolder[1]) {
printf("%sFAILED.\n", KRED);
printf("%s %s's content: %d\n", KNRM, argc[1], intHolder[0]);
printf("%s %s's content: %d\n", KNRM, argc[2], intHolder[1]);
}
else printf("%sPASSED.\n", KGRN);
printf("%s4. Column matches: \n", KNRM);
fscanf(fileOne, "%d", &intHolder[0]);
fscanf(fileTwo, "%d", &intHolder[1]);
int counter = 1;
int passed = 1;
while(!feof(fileOne) && !feof(fileTwo)) {
if (intHolder[0] != intHolder[1]) {
printf("%s FAILED AT ITEM %D. ", KRED, counter);
printf("%s%s's content: %d,", KNRM, argc[1], intHolder[0]);
printf("%s %s's content: %d\n", KNRM, argc[2], intHolder[1]);
passed = 0;
}
fscanf(fileOne, "%d", &intHolder[0]);
fscanf(fileTwo, "%d", &intHolder[1]);
counter++;
}
if (feof(fileOne) && !feof(fileTwo)) {
printf("%s PARTIAL FAIL: %s was shorter than %s. Might be a missing newline at the end of file one, but check manually to be sure.\n", KRED, argc[1], argc[2]);
}
else if(feof(fileTwo) && !feof(fileOne)) {
printf("%s PARTIAL FAIL: %s was shorter than %s. Might be a missing newline at the end of file two, but check manually to be sure.\n", KRED, argc[2], argc[1]);
}
if (passed) printf("%s PASSED.\n", KGRN);
fclose(fileOne);
fclose(fileTwo);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment