linux下byte to byte 比较文件
2015-08-26 21:43
579 查看
#include <iostream> #include <fstream> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <memory.h> using namespace std; #define SIZE 30 #define BLOCKSIZE 4096 char path1[SIZE]; char path2[SIZE]; size_t min(size_t a,size_t b) { return a<b?a:b; } void GetInput() { cout<<"Please enter two file names!"<<endl; cout<<"file1: "; cin>>path1; while(access(path1,F_OK)!=0) { cout<<"The file you enter does not exist,please enter again!"<<endl; cout<<"file1: "; cin>>path1; } cout<<"file2: "; cin>>path2; while(access(path2,F_OK)!=0) { cout<<"The file you enter does not exist,please enter again!"<<endl; cout<<"file2: "; cin>>path2; } } //byte to byte bool compfile(ifstream& in1, ifstream& in2,size_t &position){ ifstream::pos_type size1, size2,difPosition; size1 = in1.seekg(0, ifstream::end).tellg(); in1.seekg(0, ifstream::beg); size2 = in2.seekg(0, ifstream::end).tellg(); in2.seekg(0, ifstream::beg); size_t index = 0; while(index <=size1 && index<=size2) { position=index; char buffer1[BLOCKSIZE], buffer2[BLOCKSIZE];//BOLCKSIZE=4096 in1.read(buffer1, 1); //read 1 byte every time in2.read(buffer2, 1); if(0 != memcmp(buffer1, buffer2, 1)) return false; index++; } if(size1!=size2) return false; return true; } int main(int argc, char *argv[]) { size_t position=0; GetInput(); ifstream f1(path1); ifstream f2(path2); if (compfile(f1,f2,position)) cout<<"identical"<<endl; else { cout<<"different"<<endl; cout<<"The first location of difference: "<<position<<endl; } f1.close(); f2.close(); return 0; }
相关文章推荐
- CentOS配置VSFTP服务器
- centos 版本
- linux 根据sp和fp查调用栈
- Linux用户管理
- 使用 Linux 的 strace 命令跟踪/调试程序的常用选项
- 使用 Linux 的 strace 命令跟踪/调试程序的常用选项
- Linux 硬连接 软连接
- Linux登陆或者su - 用户慢问题
- Kali Linux中前十名的Wifi攻击工具
- centos6.7安装图文教程
- Linux脚本编写之sed用法
- Linux学习:安装CentOS 6.7以及CentOS 7.1
- 使用U盘安装Kali的曲折经历
- Linux Desktop Entry 文件深入解析
- 嵌入式linux内核制作
- LinuxI/O端口和I/O内存的访问接口
- Linux下NAO机器人开发环境搭建完全教程(Version1.0)
- 8-26-linux- 用户创建和目录,密码,家目录!
- linux下 c中怎么让才能安全关闭线程 和 linux线程退出时执行的程序(线程清理处理程序)简单例子
- Linux下C语言的调试 - gdb