I made changes to the code and it works, but another question. If I wanted to compare files and write differnce to a new file, for example:
- File1: My name is Whip
- File2: my name is KnutAndre
- File3: Andre (This is the difference between files).
My teacher told me to use strcmp and then get the output to a new file, but I don’t understand it at all. Does anyone have any clues I could try?
This is what my code looks like:
int main() { FILE *infile; FILE *infile2; FILE *outfile3; char input[255],input2[255]; char status1, status2; infile = fopen("test.txt", "r"); infile2 = fopen("test2.txt", "r"); if(infile == NULL) { printf("Can not open file 1!\n"); } else if(infile2 == NULL) { printf("Can not open file 2!\n"); } else { do { status1 = fgets(input, sizeof(input), infile); status2 = fgets(input2, sizeof(input2), infile2); if(status1 != 0){ printf("File 1: %s\n\nFile 2: %s\n\n", input, input2); int result = strcmp(input, input2); printf("\nResult = %d\n\n", result); } } while(status1 || status2); } fclose(infile); fclose(infile2); return 0; }
source share