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.
// From cplusplus.com reference examples
#include <iostream>
#include <fstream>
usingnamespace 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 = newchar [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.
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?
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.