Comparing files

May 10, 2010 at 5:46pm
Hi everyone,

A book I'm following has had me write a utility to compare the size of two files. I have checked over the code and it matches that of the book, however it seems to have an issue opening the files.

At the command line I enter in;
(I have placed the program and txt files on the desktop to reach them quick).

desktop\filescomp test test2

Where test and test2 are txt files - the program keeps producing the first error message "Cannot open file1...". Can anyone offer some advice as to what is (or I am doing) wrong.

Thankyou,

code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{
  register int i;
  
  unsigned char buf1[1024], buf2[1204];
  
  if(argc!=3) {
    cout << "Uasge: compfiles <file1> <file2>\n";
    return 1;
  }
  
  ifstream f1(argv[1], ios::in | ios::binary);
  if(!f1) {
    cout << "Cannot open file1...\n";
    return 1;
  }

  ifstream f2(argv[2], ios::in | ios::binary);
  if(!f2) {
    cout << "Cannot open file2...\n";
    return 1;
  }

  // Code which compares the files.
  cout << "Comparing files...\n";
  
  do {
    f1.read((char *) buf1, sizeof buf1);
    f2.read((char *) buf2, sizeof buf2);
    
    if(f1.gcount() != f2.gcount()) {
      cout << "Files are of differing sizes.\n";
      f1.close();
      f2.close();
      return 0;
    }
    
    // compare contents of buffers
    for(i=0; i<f1.gcount(); i++)
      if(buf1[i] != buf2[i]) {
        cout << "Files differ.\n";
        f1.close();
        f2.close();
        return 0;
    }
    
  } while(!f1.eof() && !f2.eof());
  
  cout << "The files are the same.\n";
  
  f1.close();
  f2.close();
  
  return 0;
}
May 10, 2010 at 5:51pm
Your text files are named "test" and "test2", and not "test.txt" and "test2.txt"?
May 10, 2010 at 5:52pm
you say that they are text files.
Are the actual names test.txt and test2.txt
May 10, 2010 at 5:56pm
Thanks for your replies, the files were originally named "test" and "test2" I changed them to "test.txt" and "test2.txt" but the issue persists.
May 10, 2010 at 6:12pm
closed account (S6k9GNh0)
Extensions are just for show. That, as it already shows, does not matter.

You can measure the size of a file using the file io pointer provided with fstream.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// From cplusplus.com reference examples
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  int length;
  char * buffer;

  ifstream is;
  is.open ("test.txt", ios::binary );

  // get length of file:
  is.seekg (0, ios::end);
  length = is.tellg();
  is.seekg (0, ios::beg);

  // allocate memory:
  buffer = new char [length];

  // read data as a block:
  is.read (buffer,length);

  is.close();

  cout.write (buffer,length);

  return 0;
}


This shows a method to get the length of a file document. Length should be the basic size of the file. We can create a basic function using this method.

For POSIX OSes, we can use the stat() function to grab certain information including filesize given with the file.
Last edited on May 10, 2010 at 6:14pm
May 10, 2010 at 6:16pm
Thanks for the response computerquip - I knew about the extensions but thought "what the heck". Also thankyou for the program provided, but I'd be very interested in your ideas as to why you think the program originally mentioned doesn't work? It keeps failing to open the fist file... how could I go about fixing that?
May 10, 2010 at 6:24pm
I bet you have the "Hide extension for known file types" option switched on in Explorer File View options and that is what is confusing you.
May 10, 2010 at 6:29pm
That option was indeed on (turned it off now) but the issue remains. Am I calling the program incorrectly?

The full thing goes like this:

C:\Documents and Settings\Jamie>desktop\compfiles test test2
Cannot open file1...
May 10, 2010 at 6:37pm
It works for me.
I have he exe and the two text files on the desktop.
I copen a command window.
I do change Directory to my desktop directory (this is to ensure that the working directory for the
program is the desktop - which isan important point);

I then enter the name of the exe and the two txt file name and it runs.

So make sure the current/working directory is correct.
May 10, 2010 at 8:27pm
Nevermind all - I found an answer to the problem, thankyou for taking the time to help me though!
Last edited on May 10, 2010 at 9:55pm
May 11, 2010 at 1:20am
Ah, I bet you were in a different working directory than the program. Alas.
Topic archived. No new replies allowed.