Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to compare two text files in C++

1 Answer

0 votes
#include <iostream>
#include <fstream>

using std::cout;
using std::endl;

int main()
{
	unsigned char buf1[1024] = { NULL }, buf2[1024] = { NULL };

	std::ifstream ifs("d:\\data.txt");
	ifs.read((char *)buf1, sizeof buf1);
	int size1 = ifs.gcount();
	ifs.close();
	ifs.clear();

	ifs.open("d:\\data.bak");
	ifs.read((char *)buf2, sizeof buf2);
	int size2 = ifs.gcount();
	ifs.close();

	if (size1 != size2) {
		cout << "Files not equal" << endl;
		return 0;
	}

	int i = 0;
	while (i < size1 && buf1[i] == buf2[i++]);

	if (i < size1) {
		cout << "Files not equal" << endl;
		return 0;
	}

	cout << "Files equal" << endl;

	return 0;
}

/*
run:

Files equal

*/

 



answered Jun 9, 2018 by avibootz

Related questions

1 answer 124 views
124 views asked Jul 8, 2020 by avibootz
1 answer 187 views
1 answer 161 views
1 answer 166 views
166 views asked Jun 10, 2018 by avibootz
1 answer 104 views
...