#include<stdio.h> #include<stdlib.h> #include<string.h> int N = 552; int main() { FILE *in,*out; char a[14][12]; char ch[12]; int i,j,p;//p保存读文件的位置,以备后用 //打开所读文件 if( ( in=fopen("E:\\201312 learning\\GPS\\gpshis100922\\gpshis100922.TXT","r") )==NULL ) { printf("cannot open this infile\n"); exit(0); } char *name; j=48; while ( !feof(in)&&j<N ) { j++; fscanf(in,"%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],a[10],a[11],a[12],a[13]);//不能有空格,否则按照scanf()处理字符串, for(i=0;i<12;i++)//ch 保存a[1](即车牌号),并依此为基准进行数据分离,并以ch为输出文件名, { ch[i]=a[1][i]; } //printf("%d\n",ftell(in)); p=ftell(in);//记录下一次循环的开始位置 name=(char *)&j; //printf("%s",name); //打开所写文件 if( ( out=fopen(ch,"w") )==NULL ) { printf("cannot open this outfile\n"); exit(0); } while ( !feof(in) ) { fscanf(in,"%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],a[10],a[11],a[12],a[13]); if(strcmp(a[1],ch)==0)//如果与ch记录相同,进行输入。 { fprintf(out,"%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],a[10],a[11],a[12],a[13]);//屏幕输入中文乱码,因为是数组不够长,一个汉字两个字节;但是,可以写入文件 } // printf("%d\n",ftell(in)); } fclose(out); fseek(in,p,0);//文件指针定位进入内层while之前的位置 printf("%d\n",ftell(in)); } printf("over\n"); fclose(in); return 0; }