
Программа сравнения файлов
Январь 3, 2011Следующая программа сравнивает два файла, используя функции файловой системы С++ reaf(), eof(), gcount(). Исходный код:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
register int i;
unsigned char buf1[1024], buf2[1024];
ifstream f1(“file1.txt”, ios::in | ios::binary);
if(f1)
{
cout << “Unable to open file.\n”;
return 1;
}
ifstream f2(“file2.txt”, ios::in | ios::binary);
if(f1)
{
cout << “Unable to open file.\n”;
return 1;
}
cout << “File comparison…\n”;
do {
f1.read((char *) buf1, sizeof buf1);
f2.read((char *) buf2, sizeof buf2);
if(f1.gcount() != f2.gcount())
{
cout << “Files have different sizes.\n”;
f1.close();
f2.close();
return 0;
}
} while(!f1.eof() && !f2.eof());
cout << “Files are the same.\n”;
f1.close();
f2.close();
cin.get();
return 0;
}