I want to compare two text files and output the lines that are different + the number of the line where two lines are different.The lenght of the line is limited to 80 characters. The two text files are passed as a command line arguments.
Here is my code, the thing compiles ok, but the result's are not what I have expected. The program I tried to write was to read only 10 lines and compare them I don't know yet how to compare the lines until the files reach their end...gotta do something with EOF constant...okej here is my code.
The output is kind of strange: For example if I put these three lines in one fileCode:#include<stdio.h> #include<stdlib.h> #include<string.h> int main (int argc, char *argv[]) { char line1[]=""; //here wil be stored the lines of a first file char line2[]=""; //..of the second one int line_number=0; FILE *dat1, *dat2; if(argc<3){ printf("You have to input two arguments!\n"); exit(1); } dat1=fopen(argv[1],"r"); //opening each file for reading dat2=fopen(argv[2],"r"); while(line_number<=10){ fgets(line1,80,dat1); //gets the first 80 characters of a line in dat1 and //stores them in line1 fgets(line2,80,dat2); //same here ...stores the line from dat2 into line2 if(strcmp(line1,line2)!=0) //compare two strings, if they are not equal... { printf("These lines don't match:\n"); line_number++; printf("%s AND %s \t at line number %d\n",line1,line2,line_number); } else line_number++; //we still have to increment the line number, //even though the two lines are equal } fclose(dat1); fclose(dat2); }
Simon
Says
Go Home
and I put the same three in the other one I get something like this:
Looks like that the first character at the first file is always cut off. Why?Code:These lines don't match: imon AND Simon at line number :1 ays AND Says at line number:2 o Home AND Go Home at line number:3
I also want for program to compare all the lines until the end of the file. Any suggestions?
Thanks!



LinkBack URL
About LinkBacks


