h1

Программа сравнения файлов

Январь 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;
}

Добавить комментарий

Fill in your details below or click an icon to log in:

Логотип WordPress.com

You are commenting using your WordPress.com account. Log Out / Изменить )

Фотография Twitter

You are commenting using your Twitter account. Log Out / Изменить )

Фотография Facebook

You are commenting using your Facebook account. Log Out / Изменить )

Connecting to %s

Follow

Get every new post delivered to your Inbox.