📄 filecmp.cpp
字号:
// compare file a.txt and b.txt
// If file b.txt contain file a.txt result=1(OK). -1(Fail) Result=-2(Error)
// Ver1.0 by FIC Lina 2009 4/21
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
// find str is in file.If in file return the string index address =1 OK
// Not find, return = -1.
// Return -2, Error
int findstrInFile(FILE * pfile,char str[],int n)
{ FILE *pfsave;
int strlong;
char ch,readstr[80];
if(n>80)
{ printf("The Program need Update.\n");
return -2;
}
for(int i=0;i<80;i++)
{
readstr[i]='\0';
}
//strlong=strlen(str);
///////////////////////////////////////////////
rewind(pfile); /*file start*/
pfsave=pfile;
ch=fgetc(pfile);
i=0;
while(i<n) //read str from file
{
readstr[i]=ch;
if(readstr[i]!=str[i])
{ i=0;
// fgetc(pfsave);
pfile=pfsave;
}
else
{
i++;
// printf("%c",str[i]);
// getch();
}
ch=fgetc(pfile);
if(ch==EOF && i<n)
{ // printf(" Not Find .\n");
return -1;
}
}
// printf(" Find : %s\n",readstr);
return 1;
}
char loadfilestr[25][80];
int indexnub[25];
int loadfile(FILE *pfile)
{ int i,j;
char ch;
rewind(pfile);
ch=fgetc(pfile);
i=0;
while(ch!=EOF)
{ j=0;
while(ch!=EOF && ch!=10) //next line
{
loadfilestr[i][j]=ch;
j++;
ch=fgetc(pfile);
}
loadfilestr[i][j]='\0'; //string end flag
indexnub[i]=j;
ch=fgetc(pfile);
i++;
}
return i;
}
int main(int argc, char *argv[])
{
int k,rows,i,n;
FILE *pfile,*pa,*pb;
char strs[10]="1234567";
if(argc!=3)
{
printf("\nParameters error!\n");
printf("Usage: FileCmp.exe a.txt b.txt\n");
return 0;
}
if((pa=fopen(argv[1],"rt"))==0)
{
printf("can not open file: %s\n",argv[1]);
return -2;
}
if ((pb=fopen(argv[2],"rt"))==0)
{
printf("can not open file: %s\n",argv[2]);
return -2;
}
rows=loadfile(pa);
i=0;
//k=findstrInFile(pb, loadfilestr[i],7); //k=1 find,k=-1 not find
for(i=0;i<rows;i++)
{
k=findstrInFile(pb, loadfilestr[i],indexnub[i]); //k=1 find,k=-1 not find
if(k==-1)
{ printf("\n Compare Fail !\n");
return -1;
}
if(k==-2)
{ printf("\n Error \n");
return -2;
}
//getch();
}
printf("\n Compare OK \n");
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -