473,545 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why Isn't File::Compare Not Working?

57 New Member
I am new to Perl and new to this site. I have the same question that I keep seeing, but not finding an answer…why doesn’t the compare function work? I’ve been going at this for a while. My code is (I’m using ActviePerl 5.8.8 Build 822 on Microsoft WinXP):

Expand|Select|Wrap|Line Numbers
  1. use File::Compare;
  2.  
  3. if (compare("$file1" , "$file") == 0)
  4. {
  5. print REPORT_FILE "Pass - Files are the same\n";
  6. }
  7. else {print REPORT_FILE "Fail - Files are different\n"};
  8.  
I deliberately made the text for the two files exactly the same but it always prints out Fail. It's almost like, the IF statement is not being recognized at all. Is there a problem with the compare function? Is there another way to compare the text of two different files to see if they are the same?

Help please!!
Sep 24 '07 #1
8 2584
eWish
971 Recognized Expert Contributor
This works for me. I would make sure that you have the proper path to the text files.
Expand|Select|Wrap|Line Numbers
  1.  use File::Compare;
  2.  
  3.         if (compare($file1, $file2) == 0) {
  4.             print "Files are equal\n";
  5.         } else {
  6.             print "Files are not equal\n";
  7.         }
Sep 24 '07 #2
numberwhun
3,509 Recognized Expert Moderator Specialist
This works for me. I would make sure that you have the proper path to the text files.
Expand|Select|Wrap|Line Numbers
  1.  use File::Compare;
  2.  
  3.         if (compare($file1, $file2) == 0) {
  4.             print "Files are equal\n";
  5.         } else {
  6.             print "Files are not equal\n";
  7.         }
I agree with eWish. I would also make sure that you please post the rest of your code so we can see it as well. I say that because above, the $file1 and $file2 variables are not set in your code. If this is all of your code, well then that is your issue.

Also, when placing code in your postings, please be sure to wrap the code in code tags as shown in the REPLY GUIDELINES to the right of the Message window.

Regards,

Jeff
Sep 24 '07 #3
Perl Beginner
57 New Member
Thanks for your responses so far. There is a whole lot more code, but here is the portion that is relevant. I added a print command that will print the text of each file in the cmd.exe window, so i know that the code is finding them in their respective directories. I hope i didnt' forget to add any lines that are relevant...


Expand|Select|Wrap|Line Numbers
  1. use File::Compare;
  2.  
  3. $known_message_dir = "C:\\Test_Input\\Known_Messages\\";
  4. $result_message_dir = "C:\\Test_Input\\Result_Messages\\";
  5.  
  6. @result_files = <'$result_message_dir\\*'>;
  7.  
  8. for $i (0...$#{ testcase{output_message} } )
  9. {
  10.     foreach $file (@result_files)
  11.     {
  12.         my $file1 = $known_message_dir.$testcase{output_message}[$i];
  13.  
  14.         open(FILE, "< $file1" );
  15.         while ( <FILE> )
  16.         {
  17.             print;
  18.         }
  19.         close FILE;
  20.  
  21.         open(FILE, "< $file" );
  22.         while ( <FILE> )
  23.         {
  24.             print;
  25.         }
  26.         close FILE;
  27.  
  28.         if (compare("$file1" , "$file") == 0)
  29.         {
  30.         print REPORT_FILE "Pass - Files are the same\n";
  31.         }
  32.         else {print REPORT_FILE "Fail - Files are different\n"};
  33.     }
  34. }
Sep 25 '07 #4
numberwhun
3,509 Recognized Expert Moderator Specialist
Thanks for your responses so far. There is a whole lot more code, but here is the portion that is relevant. I added a print command that will print the text of each file in the cmd.exe window, so i know that the code is finding them in their respective directories. I hope i didnt' forget to add any lines that are relevant...

Expand|Select|Wrap|Line Numbers
  1. use File::Compare;
  2.  
  3. $known_message_dir = "C:\\Test_Input\\Known_Messages\\";
  4. $result_message_dir = "C:\\Test_Input\\Result_Messages\\";
  5.  
  6. @result_files = <'$result_message_dir\\*'>;
  7.  
  8. for $i (0...$#{ testcase{output_message} } )
  9. {
  10.     foreach $file (@result_files)
  11.     {
  12.         my $file1 = $known_message_dir.$testcase{output_message}[$i];
  13.  
  14.         open(FILE, "< $file1" > );
  15.         while ( <FILE> )
  16.         {
  17.             print;
  18.         }
  19.         close FILE;
  20.  
  21.         open(FILE, "< $file" > );
  22.         while ( <FILE> )
  23.         {
  24.             print;
  25.         }
  26.         close FILE;
  27.  
  28.         if (compare("$file1" , "$file") == 0)
  29.         {
  30.         print REPORT_FILE "Pass - Files are the same\n";
  31.         }
  32.         else {print REPORT_FILE "Fail - Files are different\n"};
  33.     }
  34. }
  35.  
Maybe its just me, but the syntax of your for loop just does not look correct. You may want to take a read on this documentation.

Regards,

Jeff
Sep 25 '07 #5
Perl Beginner
57 New Member
I'm pretty positive that the first FOR loop does the same thing as the second FOR loop. I haven't had any problems with this FOR loop.

Expand|Select|Wrap|Line Numbers
  1. for $i (0...$#{ testcase{output_message} } )
  2. {
  3. }
  4.  
  5. for ( my $i = 0; $i <= $#{ testcase{output_message} }; $i++ )
  6. {
  7. }

Terra
Sep 25 '07 #6
KevinADC
4,059 Recognized Expert Specialist
if the files are not too big attach them to a post.
Sep 25 '07 #7
Perl Beginner
57 New Member
Well, it looks like i figured out what the problem was. Even though the text for both of the files were exactly the same, there was something in one of my files that the code saw that i couldn't see....somethin g must have been in the white space (if that makes sense). I decided to copy the text and put it in a totally new text file and the comparison worked just fine. So it turns out that my code was fine...it was the text file itself.

Thanks everyone for helping me figure this out!! This is a great site!
Sep 25 '07 #8
numberwhun
3,509 Recognized Expert Moderator Specialist
Well, it looks like i figured out what the problem was. Even though the text for both of the files were exactly the same, there was something in one of my files that the code saw that i couldn't see....somethin g must have been in the white space (if that makes sense). I decided to copy the text and put it in a totally new text file and the comparison worked just fine. So it turns out that my code was fine...it was the text file itself.

Thanks everyone for helping me figure this out!! This is a great site!
You may want to throw that file into either vi on the Unix system or into Word on Windows (with paragraph markings turned on) so you can see hidden characters and see what was going on. Just a thought.

Either way, glad its working for you.

Regards,

Jeff
Sep 25 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2108
by: jyoti | last post by:
I am new in asp. I have two html files. I like to write code to compare those two files. I like to show the difference as highlighted area. Example: File 1 contains: <html><body> Compare Test</body></html> File 2 contains: <html><body> Compare Test modyfied</body></html> I like to make either the word "modyfied" highlighted or
4
2075
by: roni | last post by:
hi. is there a tool that compare 2 vs.net projects and show the differences in code ? (in text..) such tool will help me alot to see changes i made between backups for example. have a nice day.
5
3104
by: PyPK | last post by:
I have two files file1 in format <id> <val1> <test1> <test2> 'AA' 1 T T 'AB' 1 T F file2 same as file1 <id> <val1> <test1> <test2> 'AA' 1 T T 'AB' 1 T T
2
621
by: RitaG | last post by:
Hi. In a VB.Net program I use File.Copy to copy some zipped (and text) files from one server to another. I'm required by management to do a file compare after the copy has completed. Is the File.Copy sufficient and it will error out if there's a problem or do I need to do a file compare to make sure nothing went wrong in the copy? If I...
1
8607
by: TOI DAY | last post by:
Hi All, This is what I want to do. Support I have two file name abc.txt, xyz.txt I did created md5 hash for abc.txt and store it some directory Here is how I create md5 for the abc.txt FileStream file1 = new FileStream(abc.txt, FileMode.Open, FileAccess.Read); MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
4
1449
by: aualias | last post by:
Hi, Using ASP.NET, how can I compare my local version of a file to a version on the server? Another developer made changes to the file that I am working on and I do not want to lose (well, I sort of do...) his changes. Thanks.
1
2936
by: anithas | last post by:
I need to write a C program to compare two files not in the same sequence. ie. Take one line from first file....compare with all lines in second file....If there is no match....put it in a third file If there is any match ...leave that line...and go to the second line in the first file....again compare with all lines in second file. ...
2
1201
by: dev121 | last post by:
Hi all, I have attempted to create a vb.net application that can extract neccessary data from a xml file to txt format. This is all working fine. The next step for me is to be able to scan a default folder for new files using a timer. I understand how the timer will work but need obviously to know how to scan a folder for files. which...
12
7513
by: Liz | last post by:
Does anyone know of a good source file compare utility, with or without merge capabilities?
0
7393
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7653
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7803
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7411
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7749
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5965
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3444
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
695
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.