Thread: Comparing two files

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Comparing two files

    Can someone tell me why isn't my code working?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ()
    {
    	FILE *f1;
    	FILE *f2;
    	char c1, c2;
    	int cmp;
    
    	f1 = fopen("file1.txt", "r");
    	f2 = fopen("file2.txt", "r");
    
    	if (f1 == NULL || f2 == NULL)
    	{
    		printf("File access error.\n");
    		exit(1);
    	}
    
    	else
    	{
    	    while (fgetc(f1) != EOF || fgetc(f2) != EOF)
    	    {
                c1 = fgetc(f1);
                c2 = fgetc(f2);
    
                if (c1 != c2)
                {
                    printf("CODE 1: Files are not equal.\n");
                    cmp = 1;
                    break;
                }
    	    }
    
            if (cmp == 0)
    		printf("CODE 0: Files are equal.\n");
    	}
    
        if (fclose(f1) != 0 || fclose(f2) != 0)
        printf("File close error.\n");
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Define "not working". That's a pretty ridiculous way to ask for help. We're not standing over your shoulder looking at the output.

    I will say this:
    Code:
    while (fgetc(f1) != EOF || fgetc(f2) != EOF)
    That's not the right way to do it. For one thing, you're throwing away a character from f1 with each loop.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    The program only does one thing, so basically it doesn't do the only thing it's supposed to do, which is comparing files. It keeps telling me the files are not equal even if I copy pasted the files used.

    I don't really understand what you mean. I meant for the loop to stop once a file reaches the EOF.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    It's because of EXACTLY what I said.

    Code:
    while (fgetc(f1) != EOF || fgetc(f2) != EOF)
    That first does an fgetc on the file f1 and THROWS away the result. If it's not EOF, then the second half of that OR does not get executed due to short circuiting (if the first statement in an OR is true, what's the point of evaluating the second, right? It's this OR that, and if this is true it doesn't matter whether that is true, because the OR condition is satisfied).

    f1 now is pointing at the second character in the file, while f2 is pointing at the first.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Ah, I didn't realize you literally meant it to be thrown away. I thought it was increasing the sequence. Sorry, my bad.

    I think I got it. I changed the middle part to this:

    Code:
    	else
    	{
    	    while (1)
    	    {
                c1 = fgetc(f1);
                c2 = fgetc(f2);
    
                if (c1 != c2)
                {
                    printf("CODE 1: Files are not equal.\n");
                    break;
                }
    
                else if (c1 == EOF || c2 == EOF)
                {
                printf("CODE 0: Files are equal.\n");
                break;
                }
    	    }
    	}
    I'm still checking if the algorithm works correctly, but in the meantime, thanks for the help!
    Last edited by 843; 11-24-2010 at 11:51 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    c1 and c2 should be of type int, not char, because fgetc returns and int and EOF is a negative value of type int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Ah, I didn't know that fgetc returns an int. Thanks, I checked and you're right. Strangely, the program works as is and my lecturer actually declares it as char. Any idea why?

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    The only reason fgetc is defined as returning an integer is because EOF is defined as a negative integer. Signed char happens to store small negative integers. I have not seen an implementation that breaks if you use char, but that doesn't make it right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error opening files in a different dir
    By Ozzie in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2008, 06:55 AM
  2. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM