compare two files in C and print the line when they do not match
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
compare two files in C and print the line when they do not match
I have commented what I think this should do. The problem is the third file that I read and try to put it's contents into fpread2. The file contents aren't getting there. I don't know if I am using that string compare right either. Thanks for any input!
Code:
/* The program compares two text files specified at the command prompt.*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINE 150
int main(int argc, char *argv[]) {
FILE *fpread, *fpread2; /* File Pointer Read, File Pointer Read */
char filebuff[MAXLINE];
char filebuff2[MAXLINE];
int var;
if (argc != 3) {
printf("Usage: %s source destination\n", argv[0]);
exit(0);
}
fpread = fopen(*++argv, "r");/* opens second file which is read */
if (fpread == NULL) {
printf("Can't open %s for reading\n", *argv);
exit(0);
}
fpread2 = fopen(*++argv, "r");/* opens third file which is also read */
if (fpread2 == NULL) {
printf("Can't open %s for reading\n", *argv);
exit(0);
}
while (fgets(filebuff, MAXLINE, fpread) != NULL) { /* put the contents of fpread(second file) into filebuff */
fgets(filebuff2, MAXLINE, fpread2); /* put the contents of fpread2(third file) into filebuff2 */
var = strcmp(filebuff, filebuff2); /* compare the two files one line at a time */
if ((var < 0) || (var >0)) /* var should be 0 if they are the same */
printf("\nvar:%i\n", var);
printf("this is where the files do not match:\nfilebuff:%s\n", filebuff);
printf("filebuff2:%s\n", filebuff2);
exit(0);
/* fputs(filebuff, fpread2); write the contents of filebuff to fpread2(third file) */
/* printf("filebuff:\n %s", filebuff); need to compare 2nd and 3rd file */
}
fclose(fpread);
fclose(fpread2);
return(0);
}
if ((var < 0) || (var >0))
{
printf("\nvar:%i\n", var);
printf("this is where the files do not match:\nfilebuff:%s\n", filebuff);
printf("filebuff2:%s\n", filebuff2);
exit(0);
}
Also, any reason why you're comparing var using < and > instead of !=?, i.e.
Code:
if (var != 0)
{
printf("\nvar:%i\n", var);
printf("this is where the files do not match:\nfilebuff:%s\n", filebuff);
printf("filebuff2:%s\n", filebuff2);
exit(0);
}
No, I just haven't programmed that much. I am a Comp. Sci. major trying to figure this crap out. I did figure it out. Why didn't anyone tell me to use strcmp and buffer both file's content to compare them against each other? I thought you guys were the hacks of all hacks! Anyways here is the revised code in case any one else has this problem.
Code:
FILE *fpread, *fpread2;
char filebuff[MAXLINE];
char filebuff2[MAXLINE];
int var = 0;
int linecount = 0;
if (argc != 3) {
printf("Usage: %s source destination\n", argv[0]);
exit(0);
}
fpread = fopen(*++argv, "r");/* opens second file which is read */
if (fpread == NULL) {
printf("Can't open %s for reading\n", *argv);
exit(0);
}
fpread2 = fopen(*++argv, "r");/* opens third file which is also read */
if (fpread2 == NULL) {
printf("Can't open %s for reading\n", *argv);
exit(0);
}
while (((fgets(filebuff, MAXLINE, fpread)) && (fgets(filebuff2, MAXLINE, fpread2))) != NULL) {
++linecount;
var = strcmp(filebuff, filebuff2);
if (var != 0){
printf("\nThe files differ on line %i.\n", linecount);
printf("This line in the first file doesn't match the second file:\n%s\n", filebuff);
fclose(fpread);
fclose(fpread2);
exit(0);
}
}
printf("The files are identical");
fclose(fpread);
fclose(fpread2);
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.