Ryan的博客

半路出家,多多关照

QT 比较两个文件是否一致(不限格式)

所谓不限格式就是讲文件以二进制的格式打开,逐位进行比较。

/******************************************************
说明:比较两个消息文件是否一致,如果一致返回0
输入:待比较消息的路径
***********************************************************/
int CompareMsg(char* msg1, char* msg2);

CompareMsg(char* msg1, char* msg2)
{
	//将2个消息各自读取到buffer中
	FILE *pOne, *pTwe;
	long lsize1, lsize2;
	char* buffer1 = NULL;
	char* buffer2 = NULL;
	size_t result1, result2;

	pOne = fopen(msg1, "rb");
	pTwe = fopen(msg2, "rb");
	if ((pOne == NULL) || (pTwe == NULL))
	{
		fclose(pOne);
		fclose(pTwe);
		return 1;
	}

	fseek(pOne, 0, SEEK_END);
	lsize1 = ftell(pOne);
	//rewind(pOne);
	fseek(pOne, 0, SEEK_SET);
	fseek(pTwe, 0, SEEK_END);
	lsize2 = ftell(pTwe);
	rewind(pTwe);

	if (lsize1 != lsize2)
	{
		fclose(pOne);
		fclose(pTwe);
		return 1;
	}

	buffer1 = new char[lsize1];
	buffer2 = new char[lsize2];
	if ((buffer1 == NULL) || (buffer2 == NULL))
	{
		fclose(pOne);
		fclose(pTwe);
		delete[]buffer1;
		delete[]buffer2;
		return 1;
	}

	result1 = fread(buffer1, 1, lsize1, pOne);
	result2 = fread(buffer2, 1, lsize2, pTwe);
	if ((result1 != lsize1) || (result2 != lsize2))
	{
		fclose(pOne);
		fclose(pTwe);
		delete[]buffer1;
		delete[]buffer2;
		return 1;
	}


	int result = memcmp(buffer1, buffer2, lsize1);

	fclose(pOne);
	fclose(pTwe);
	delete[]buffer1;
	delete[]buffer2;

	return result;
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_30440627/article/details/53978636
个人分类: Qt学习记录
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭