📄 comp.c
字号:
/******************************************************************
Write by by chenxu 2001-2-15
This program is compare two TXT file which genarated by conv.exe
and find out those difference and output SQL script.
Usage: comp 1.txt 2.txt out.sql dest_table
******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#define MAX_ROW_LENGTH 1200
#define MAX_FIELD_NUMBER 100
FILE * oldfp;
FILE * newfp;
FILE * outfp;
FILE * datafp;
int serno_pos;
/*************************************************************
Function: Trim()
删除字符串前后的空格
*************************************************************/
void trim(char * str, char * output)
{ char ret[120];
int len;
strcpy(ret ,str);
while (ret[0] == ' ')
{
strcpy(ret, &ret[1]); //去前面的空格
};
len = strlen(ret);
while(ret[len-1]==' ') //去后面的空格
{
ret[len-1]='\0';
len = strlen(ret);
};
strcpy(output, ret);
return;
}
/*************************************************************
Function: upstring()
将字符串转换成大写
*************************************************************/
void upstring(char * str, char * output)
{
char tmpstr[255];
int len,i;
strcpy(tmpstr, str);
len = strlen(tmpstr);
for (i=0; i<len; i++) {
if (isalpha(tmpstr[i])!=0) {
tmpstr[i] = toupper(tmpstr[i]);
}
}
strcpy(output,tmpstr);
}
/***********************************************************
int get_serno_pos()
Function: find out the pos of "serno" field in file string.
************************************************************/
int get_serno_pos(char *filestr)
{
serno_pos = strlen(filestr)-1;
return serno_pos;
}
unsigned long get_serno(char *string)
{
char *p;
char str[20];
unsigned long serno;
p = string + serno_pos-1;
strcpy(str, p);
printf("serno str =%s", str);
serno = atol(str);
return serno;
}
int main(int argc, char **argv)
{
char newbuf[MAX_ROW_LENGTH];
char oldbuf[MAX_ROW_LENGTH];
char newfile[80], oldfile[80], outfile[80], desttable[30], out_sql[80];
unsigned long i;
char out_txt[80];
unsigned long iRow=0;
char s_no[20];
unsigned long iSerno;
char cmd[200];
// the data output filename
if (argc!=5)
{
printf("\nUsage : comp old.txt new.txt out.sql dest_table\n") ;
return -1;
}
strcpy(oldfile, argv[1]);
strcpy(newfile, argv[2]);
strcpy(out_sql, argv[3]);
strcpy(desttable,argv[4]);
strcpy(out_txt, "comp.out");
if ((newfp=fopen(newfile,"r+")) == NULL)
{
printf("Cannot open file %s\n", newfile);
return -1 ;
}
if ((oldfp=fopen(oldfile, "r+")) == NULL)
{
printf("Cannot open file %s\n", oldfile);
return -1;
}
if ((outfp=fopen(out_sql, "w+")) == NULL)
{
printf("Cannot open file %s\n", outfile);
return -1;
}
if (( datafp = fopen(out_txt,"w+")) == NULL)
{
printf("Cannot open file %s\n", out_txt);
return -1;
}
while (!feof(newfp) && !feof(oldfp) )
{
fgets(newbuf, MAX_ROW_LENGTH, newfp);
fgets(oldbuf, MAX_ROW_LENGTH, oldfp);
if (iRow < 1) {
get_serno_pos(oldbuf);
iRow ++;
}
if ( strcmp(newbuf, oldbuf)!=0 ) {
iSerno = get_serno(newbuf);
// Write Delete SQL to out.sql
fprintf(outfp, "delete from %s where serno = \'%ld\';\ncommit;\n",desttable, iSerno);
printf("\n1 record found, serno = %ld", iSerno);
// Write Insert DATA to comp.out
fprintf(datafp, "%s", newbuf);
}
}
fclose(oldfp);
fclose(newfp);
fclose(outfp);
fclose(datafp);
// delete old.txt
sprintf(cmd, "rm %s", oldfile);
system(cmd);
// rename <new>.txt to <new>.old
sprintf(cmd, "mv %s %s", newfile, oldfile);
system(cmd);
// rename comp.out <new>.txt
sprintf(cmd, "mv %s %s", out_txt, newfile);
system(cmd);
return 0 ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -