- 论坛徽章:
- 0
|
求:一段小程序
- #include <stdio.h>;
- #include <string.h>;
- #include <stdlib.h>;
- #define USE_CMP "country: CN"
- int count_s = 0, sum_s = 0,block = 0;
- //读行函数start
- char * readline(FILE *frd)
- {
- int i;
- char *tmp, tmp_char,tmp_array[100];
- int j = 0;
- extern count_s, block, sum_s;
-
- while((tmp_char = fgetc(frd)) != '\n')
- {
- if ( feof(frd)!=0 )
- {
- return NULL;
- }
-
- tmp_array[j] = tmp_char;
- j++;
-
- }
-
- if (j==0)
- block = 1;
-
- count_s = j;
- tmp = (char *)calloc(j,sizeof(char));
- for (i = 0; i < j; i++ )
- *(tmp+i) = tmp_array[i];
- return tmp;
- }
- //读行函数end
- //读块函数start
- char * readblock(FILE *fp)
- {
- char *init, *tmp1, *tmp2;
- int count_i, sum_tmp;
- extern sum_s, count_s, block;
- while( block != 1 )
- {
- sum_tmp = sum_s;//保存上一次的字符串长度!
- count_s = 0;//清零
- tmp1 = readline(fp);//读取下一行
- if (tmp1 != NULL)
- {
- sum_s += count_s;
- tmp2 = (char *)calloc(sum_s+1,sizeof(char));
- if ( sum_s != 0 )
- {
- for(count_i = 0; count_i < sum_tmp;count_i++)//初始化原来的元素!
- *(tmp2 + count_i) = *(init + count_i);
- free(init);
- }
-
- for(count_i = sum_tmp; count_i < count_s + sum_tmp;count_i++)
- *(tmp2 + count_i) = *(tmp1 + count_i - sum_tmp);
- *(tmp2+count_i)='|';
-
- init = (char *)calloc(sum_s+1,sizeof(char));
- for(count_i = 0; count_i < sum_s+1;count_i++)
- *(init + count_i) = *(tmp2 + count_i);
- sum_s++;
- free(tmp1);
- free(tmp2);
- }
- else
- break;
- };
- tmp1 = (char *)calloc(sum_tmp-1,sizeof(char));
- for(count_i = 0; count_i < sum_tmp-1;count_i++)
- *(tmp1 + count_i) = *(init + count_i);
- *(tmp1 + count_i) = '\0';
- free(init);
- init = tmp1 ;
- return init;
- }
- //读块函数end
- int compare_s(char *cmp)
- {
- int i, start_c = 0, end_c = 0, j, cmp_stat = 0, len_cmp;
- char *cmp_source;
-
- cmp_source = (char *)calloc(strlen(USE_CMP),sizeof(char));
- strcpy(cmp_source,"country: CN");
- len_cmp = strlen(cmp_source);
- for (i = 0; i < sum_s; i++)
- {
- if ( (*(cmp+i) == '|')&&(*(cmp+i+12) == '|') )
- for (j = 0; j < len_cmp; j++)
-
- if ( *(cmp+j+i+1) != cmp_source[j] )
- {
- cmp_stat = 0;
- break;
- }
- else
- {
- cmp_stat = 1;
- }
- }
- return cmp_stat;
- }
- int main()
- {
- FILE *fp,*wt;
- int result, find_stat = 0, i;
- char *tmp1;
- extern count_s, sum_s, block;
- fp = fopen("log1.dat","r");
- wt = fopen("out.txt","w+");
- while( feof(fp) == 0 )
- {
- tmp1=readblock(fp);
- result = compare_s(tmp1);
- if (result==1)
- {
- find_stat++;
- for( i = 0; i < sum_s; i++)
- {
- switch ( *(tmp1+i) )
- {
- case '|': fputc('\n',wt);break;
- case '\0': fputc('\n',wt);break;
- default: fputc( *(tmp1+i) ,wt);break;
- }
- }
- fputc('\n',wt);
- }
- free(tmp1);
- block=sum_s=0;
- }
- fclose(fp);
- printf("\n++++FIND RESULT: %d\n",find_stat);
- return 0;
- }
复制代码 |
|