반응형
replace
[CF-1-3.1] /* author : kyoung chip jang date : descript: */ #include#include int main( int argc , char** argv ) { FILE* fpr,*fpw; char bufr[256],bufw[256]; char str1[] = "dog"; char str2[] = "rabbit"; char *p , *q; if( !(fpr=fopen("dog.txt","r"))) { printf(" cant file open\n"); return 1; } if( !(fpw=fopen("rabbit.txt","w"))) { printf(" cant file open\n"); return 1; } while(1) { fgets( bufr , 256 , fpr ); if( bufr[0] == '\0' ) break; printf( "%x \n" , bufr[0] ); strcpy( bufw, bufr ); p = strstr( bufr ,str1 ); if(p) { q = bufw + ( p - bufr ); strcpy( q , str2 ); strcpy( q + strlen(str2) , p+strlen(str1)); } fprintf(fpw , "%s", bufw); if( feof( fpr ) ) { printf(" bufr = %s \n" , bufr ); break; } bufr[0] = '\0'; } fclose(fpr); fclose(fpw); return 0; } [CF-1-3.2] /* author : kyoung chip jang date : descript: */ #include #include void c_replace( char *desc , char *src , char * desc_str , char * src_str ); int main( int argc , char** argv ) { FILE* fpr,*fpw; char bufr[256],bufw[256]; char str1[] = "dog"; char str2[] = "rabbit"; if( !(fpr=fopen("dog.txt","r"))) { printf(" cant file open\n"); return 1; } if( !(fpw=fopen("rabbit.txt","w"))) { printf(" cant file open\n"); return 1; } while(1) { fgets( bufr , 256 , fpr ); c_replace( bufw, bufr , str2 , str1 ); fprintf(fpw , "%s", bufw); if( feof( fpr ) ) { printf(" bufr = %s \n" , bufr ); break; } bufr[0] = '\0'; } fclose(fpr); fclose(fpw); return 0; } void c_replace( char *desc , char *src , char * desc_str , char * src_str ) { char *p = NULL; char *q = NULL; if( src[0] == '\0' ) return; // printf( "%x \n" , src[0] ); strcpy( desc, src ); p = strstr( src ,src_str ); if(p) { q = desc + ( p - src ); strcpy( q , desc_str ); strcpy( q + strlen(desc_str) , p+strlen(src_str)); } }
반응형