LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-11-2006, 11:51 PM   #1
calorie712
LQ Newbie
 
Registered: Feb 2006
Posts: 26

Rep: Reputation: 15
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);
}
 
Old 04-12-2006, 01:46 AM   #2
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
You forgot the braces for the if-statement:
Code:
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);
}
 
Old 04-12-2006, 03:13 AM   #3
mickyg
Member
 
Registered: Oct 2004
Location: UK
Distribution: Ubuntu/Kubuntu
Posts: 245

Rep: Reputation: 30
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);
}
 
Old 04-12-2006, 03:13 PM   #4
Last Attacker
Member
 
Registered: Jun 2004
Location: South Africa
Distribution: Ubuntu
Posts: 120

Rep: Reputation: 15
Provably worked with VB too much LOL!
 
Old 04-12-2006, 04:10 PM   #5
leosgb
Member
 
Registered: Nov 2004
Location: Brazil
Distribution: Gentoo
Posts: 363

Rep: Reputation: 31
what about diff?
 
Old 04-12-2006, 06:45 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
or comm if you can sort the files
 
Old 04-13-2006, 12:15 AM   #7
calorie712
LQ Newbie
 
Registered: Feb 2006
Posts: 26

Original Poster
Rep: Reputation: 15
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);
 
Old 04-13-2006, 01:56 AM   #8
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Quote:
Originally Posted by calorie712
Why didn't anyone tell me to use strcmp and buffer both file's content to compare them against each other?
Huh!? Maybe because... you had already figured it out in your original post? Remember?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I compare files line-by-line? spiffytech Linux - Software 2 01-05-2006 02:28 PM
i want to print all the files in a directory and the file size in a same line naveen245 Programming 3 12-06-2005 08:22 AM
File reading line by line and compare Goni Linux - Software 14 09-21-2005 12:24 AM
print files in PDF or html format from the linux command line IBKnobel Linux - Software 3 07-12-2004 09:29 PM
Command line tool to print length of mp3 files? J_Szucs Linux - Software 3 05-04-2004 05:01 AM


All times are GMT -5. The time now is 01:08 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration