Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C Program to see if two files are the same

  • 10-11-2011 3:09pm
    #1
    Closed Accounts Posts: 95 ✭✭


    Hi, another problem. This is a program to see if two files are the same or different. It compiles but regardless of whether the files are the same or different, it says the files are different (i.e. if two files contain the same information). Am I completely off beam, or is it something simple that my program is missing?

    As before, I'm really grateful for any help.
    #include<stdio.h> 
    int main()
    {
    	FILE *fp;
    	int letter;
    	int num_asterix1=0;
    	int num_asterix2=0;
    	int num_quotes=0;
    	char fname[80];
    	char fname1[80];
    	printf("Please enter the name of the first file you wish to check: ");
    	gets(fname);
    	printf("Please enter the name of the second file you wish to check: ");
    	gets(fname1);
    	fp=fopen (fname, "r");
    	fp=fopen (fname1, "r");
    		if(strcmp(fname, fname1)==0)
    		printf("The files <%s> and <%s> are identical.", fname, fname1);
    		else
    		printf("The files <%s> and <%s> are different.", fname, fname1);
    	
    return 0;
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Now at a complete guess...I think I remember something about gets leaving the \n (newline) character in the input buffer. So the next time you use it, you'll end up getting that \n.

    If you enter:
    file1.txt
    file1.txt

    fname will be "file1.txt"
    fname1 will be "\nfile1.txt"

    Again this is a guess. In the output do you see anything strange when you print them. Is there a sudden line break in the middle of your sentence. "The files <%s> and <%s> are different."


  • Closed Accounts Posts: 845 ✭✭✭yupyup7up


    The Crab wrote: »
    Hi, another problem. This is a program to see if two files are the same or different. It compiles but regardless of whether the files are the same or different, it says the files are different (i.e. if two files contain the same information). Am I completely off beam, or is it something simple that my program is missing?

    As before, I'm really grateful for any help.
    #include<stdio.h> 
    int main()
    {
    	FILE *fp;
    	int letter;
    	int num_asterix1=0;
    	int num_asterix2=0;
    	int num_quotes=0;
    	char fname[80];
    	char fname1[80];
    	printf("Please enter the name of the first file you wish to check: ");
    	gets(fname);
    	printf("Please enter the name of the second file you wish to check: ");
    	gets(fname1);
    	fp=fopen (fname, "r");
    	fp=fopen (fname1, "r");
    		if(strcmp(fname, fname1)==0)
    		printf("The files <%s> and <%s> are identical.", fname, fname1);
    		else
    		printf("The files <%s> and <%s> are different.", fname, fname1);
    	
    return 0;
    }
    

    Haven't done C in a long time, but looks to me like you are comparing the filenames as opposed to the actual stream from the fopen. Also, you are assigning fp to both fopen calls so that it is just being overwritten.

    You should have FILE *fp, *fp1; I reckon and compare fp and fp1 instead.

    As I said I'm awfully rusty with C ;)


  • Registered Users, Registered Users 2 Posts: 1,306 ✭✭✭carveone


    Well. You've successfully compared the names of the two files not their contents.


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    yupyup7up wrote: »
    Haven't done C in a long time, but looks to me like you are comparing the filenames as opposed to the actual stream from the fopen. Also, you are assigning fp to both fopen calls so that it is just being overwritten.

    You should have FILE *fp, *fp1; I reckon and compare fp and fp1 instead.

    As I said I'm awfully rusty with C ;)

    I was thinking this also but assumed it was actually the filenames he was comparing? :confused:

    Edit: Okay i read the complete post so yeah it appears he does want to compare the contents, not the names.


  • Closed Accounts Posts: 95 ✭✭The Crab


    Webmonkey wrote: »
    Now at a complete guess...I think I remember something about gets leaving the \n (newline) character in the input buffer. So the next time you use it, you'll end up getting that \n.

    If you enter:
    file1.txt
    file1.txt

    fname will be "file1.txt"
    fname1 will be "\nfile1.txt"

    Again this is a guess. In the output do you see anything strange when you print them. Is there a sudden line break in the middle of your sentence. "The files <%s> and <%s> are different."

    No, nothing strange. If I put in exactly the same file names then it says they are identical.

    I wonder is the problem strcmp - is that comparing the names of the files rather than the files themselves?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    The Crab wrote: »
    No, nothing strange. If I put in exactly the same file names then it says they are identical.

    I wonder is the problem strcmp - is that comparing the names of the files rather than the files themselves?
    It is most definitely. Think about the other thread you posted earlier today about reading the contents of the file, you'll need something along those lines.
    You'll have to do something in parallel and once something isn't the same, you'll have proven the files arn't the same.

    It's not much more work tbh.


  • Closed Accounts Posts: 95 ✭✭The Crab


    Forget the last message I put (about it not being strcmp that's the problem), I made a mistake as per that.

    Hmm, I'm a little confused now. Is it that I need a while loop comparing the inner of the file?


  • Registered Users, Registered Users 2 Posts: 1,306 ✭✭✭carveone


    The Crab wrote: »
    I wonder is the problem strcmp - is that comparing the names of the files rather than the files themselves?

    Yup. You pretty much have to rethink the whole thing :D

    Open each file. In binary if you are using Windows. Read a block from each file, compare them, repeat until end of block.

    Unless there's a CompareFileContents call... which probably isn't the point of the homework!


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    The Crab wrote: »
    Something really weird is happening. If I change the if condition to NOT EQUAL (at the moment it is at EQUAL) then the same problem happens in reverse, i.e. it says two completely different files are identical.

    At least that says the problem isn't strcmp comparing the two file names.
    Your whole logic is wrong. You are not doing what you think you are doing. It's got nothing to do with strcmp. You shouldn't even need it!


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    carveone wrote: »
    Yup. You pretty much have to rethink the whole thing :D

    Open each file. In binary if you are using Windows. Read a block from each file, compare them, repeat until end of block.

    Unless there's a CompareFileContents call... which probably isn't the point of the homework!
    They also had an assignment to read character by character each file so it's assumed text based so just simple character comparisons is all that's needed i'd say. OP re think the whole thing :)


  • Advertisement
  • Closed Accounts Posts: 845 ✭✭✭yupyup7up


    Yep, as said above, check the file stream character by character and exit if the character at the same index in both files doesnt match! Simples ;)


  • Closed Accounts Posts: 95 ✭✭The Crab


    Webmonkey wrote: »
    They also had an assignment to read character by character each file so it's assumed text based so just simple character comparisons is all that's needed i'd say. OP re think the whole thing :)

    Would a while loop through the whole text like I did before help? Then put the if and else inside that?

    C bamboozles me. Don't know why I found java easier :confused:.


  • Closed Accounts Posts: 95 ✭✭The Crab


    I'm a bit confused over how to create a while loop which compares two files at the same time.


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Something along those lines. Read both files in parallel and then use your ifs on the characters you read from both streams. Remember the fgetc. If you get to the end of file in both cases at the same time without a character being wrong, then you have proven the file is equal. If any other case, if the EOF comes earlier in any file or if the characters don't match at any stage then the file is not equal.


  • Closed Accounts Posts: 95 ✭✭The Crab


    carveone wrote: »
    Yup. You pretty much have to rethink the whole thing :D

    Does that mean I should delete the file I currently have and start from scratch?


  • Registered Users, Registered Users 2 Posts: 1,306 ✭✭✭carveone


    The Crab wrote: »
    Does that mean I should delete the file I currently have and start from scratch?

    Ah no. Just the last few lines. I'm thinking I'll let you think for a second but I'll drop some hints in a sec... (doing two things at once!)


  • Registered Users, Registered Users 2 Posts: 1,306 ✭✭✭carveone


    Webmonkey wrote: »
    Something along those lines. Read both files in parallel and then use your ifs on the characters you read from both streams. Remember the fgetc. If you get to the end of file in both cases at the same time without a character being wrong, then you have proven the file is equal. If any other case, if the EOF comes earlier in any file or if the characters don't match at any stage then the file is not equal.

    That's it exactly. fgetc returns an "int" - so it's a character (0 to 255) or an EOF for end of file. EOF is -1.

    open file1
    open file2

    int c1, c2;

    do {
    c1 = fgetc from file1
    c2 = fgetc from file2

    if (c1 != c2)
    {
    done -> files are not the same.
    }

    } while ((c1 != EOF) && (c2 != EOF))

    done -> files are the same.

    Something like that. Note that if they reach the end of the file at the same time c1 will equal c2 and that's ok.


  • Closed Accounts Posts: 95 ✭✭The Crab


    Thanks guys! You're a huge help! I have half this week's assignment done now and have tomorrow to play around with the rest, so expect me to be back annoying you tomorrow haha! :D


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Yep don't forget to do an additional check after to ensure that both are at EOF, otherwise one file ended earlier than the other and they are not even. It could happen that you get to the end of one file while the contents of both being the same up till then. So the loop will exit but you won't have got a chance to check the characters.

    Continuing from above after the loop ends do another check
    ...
    if (c1 != c2)
     // not the same.
    

    Edit: Ignore the above. Day dreaming :)


  • Registered Users, Registered Users 2 Posts: 1,306 ✭✭✭carveone


    Sure thing. C is pretty low level. Wait till you hit pointers. Muahahaha...


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,306 ✭✭✭carveone


    Webmonkey wrote: »
    Yep don't forget to do an additional check after to ensure that both are at EOF, otherwise one file ended earlier than the other and they are not even.

    Ah! But then c1 wouldn't be equal to c2 and the file wouldn't be same. Which is why I did a do {} while. :)

    Where I said "done -> files are not the same" I did intend that the loop should be broken at that point, either using exit or break;

    If you use break, you'll have to flag what happened. So you'd set flag_different = 0 before the loop and then go:
    if (c1 != c2)
    {
        flag_different = 1;
        break;
    }
    


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Damn it sorry! I'm multi tasking all day here and seem to be making a lot of mistakes on boards today.

    You are indeed right. Ignore my last post!


  • Closed Accounts Posts: 95 ✭✭The Crab


    carveone wrote: »
    Sure thing. C is pretty low level. Wait till you hit pointers. Muahahaha...

    What does it mean when people say low level and high level? Is it how java has loads of methods which makes it easier to do things (I've noticed even at this early stage that Java seems, although I could be completely wrong to have methods which you can call which does half the work for you???)


  • Registered Users, Registered Users 2 Posts: 1,306 ✭✭✭carveone


    The Crab wrote: »
    What does it mean when people say low level and high level? Is it how java has loads of methods which makes it easier to do things (I've noticed even at this early stage that Java seems, although I could be completely wrong to have methods which you can call which does half the work for you???)

    Pretty much yes. The lowest level is assembly language which is what a compiler outputs. From there you have more and more abstraction from the machine. The advantage is less dependency on the machine and (hopefully) less work for the developer. The disadvantage is speed and memory usage.

    C was created in the early 1970s for the PDP minicomputer - it's quite a low level language!


  • Closed Accounts Posts: 95 ✭✭The Crab


    Thanks again! That's good to know about java and C, that's why I'm finding java easier then.


Advertisement