C语言写的,以下是源代码:- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define MAX 2048
- void ch(char *);
- int star(char *b,FILE *a);
- int main(int argc,char *argv[3])
- {
- /*设置路径
- char *Path_a="C:\\Users\\Cool_Breeze\\Desktop\\gin\\test\\a.txt";
- char *Path_b="C:\\Users\\Cool_Breeze\\Desktop\\gin\\test\\b.txt";
- char *Path_c="C:\\Users\\Cool_Breeze\\Desktop\\gin\\test\\c.txt";
- */
- //打开文件
- FILE *fp_a,*fp_b,*fp_c;
- char a[MAX],b[MAX],c[MAX];
- if ((fp_a=fopen(*(argv+1),"r"))==NULL)
- {
- printf("%s 打开失败!",*(argv+1));
- return 0;
- }
- if ((fp_b=fopen(*(argv+2),"r"))==NULL)
- {
- printf("%s 打开失败!",*(argv+2));
- return 0;
- }
- if ((fp_c=fopen(*(argv+3),"w"))==NULL)
- {
- printf("%s 打开失败!",*(argv+3));
- return 0;
- }
- while(fgets(b,MAX,fp_b)!=NULL)
- {
- if (star(b,fp_a)==1) //写入
- {
- fprintf(fp_c,"%s",b);
- }
- }
- fclose(fp_a);
- fclose(fp_b);
- fclose(fp_c);
- return 0;
- }
- int star(char *b,FILE *a)
- {
- char b_s[MAX]; //储存第三列字符串
- char *c=b_s;
- int d=0,n=0;
- while (*(b+n++) != '\0')
- {
- if (*(b+n) == ' ')
- {
- d++;
- if (d==2) //出现第二个分隔符后拷贝第三列字符串
- {
- strncpy(c,b+n+1,MAX); //拷贝
- ch(c);
- goto A;
- }
- }
-
- }
- A:
- char temp[MAX]="0";
- fseek(a,0,SEEK_SET); //恢复文件指针
- while(fgets(temp,MAX,a)!=NULL)
- {
- ch(temp);
- if (strncmp(temp,c,MAX)==0) //比较字符串
- {
- return 1; //一样返回1
- }
- }
- return 0; //不一样返回0
- }
- void ch(char *old)
- {
- char sta;
- do
- {
- sta=*(old++);
- if (sta == '\n' || sta == '\r')
- {
- old[strlen(old)-1]='\0'; //去掉换行符
- }
- }while(sta);
- }
复制代码
|