Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define SCHSTR "#IND"
- #define std_in "<stdin>"
- //-----------------------------------------------------------------
- void usage(char* cmd);
- void chop_crlf(char* buff);
- char* search(char* text, char* key);
- //-----------------------------------------------------------------
- int main(int argc, char* argv[])
- {
- FILE *fpi,*fpo;
- int i,count;
- char *cmd, *p;
- const char* ifile = std_in;
- char* schstr = SCHSTR;
- char buf[512];
- //--------------------------------------------------
- // 引数の読込み
- //--------------------------------------------------
- /*
- cmd = argv[0];
- if ( argc<3 )
- {
- usage(cmd);
- return 0;
- }
- for ( i=1; i<argc; i++ )
- {
- if ( !strcmp(argv[i],"-s") )
- {
- if ( ++i >= argc )
- {
- usage(cmd);
- return 0;
- }
- schstr = argv[i];
- } else ifile = argv[i];
- }
- */
- if(argc!=3)
- {
- fprintf(stderr,"Usage: %s\n(1)Input_#IND_filename\n(2)Output_NON_#IND_filename\n",argv[0]);
- exit(1);
- }
- if((fpi=fopen(argv[1],"r"))==NULL)
- {
- printf ("The file can't be opened. The program is exit.\n");
- return 0;
- }
- /*
- //--------------------------------------------------
- // ファイル入力
- //--------------------------------------------------
- if ( strcmp(ifile,std_in) )
- {
- if ( ( fp = fopen(ifile,"r") ) == NULL )
- {
- fprintf(stderr,"No such file or directory.\n");
- return -1;
- }
- }
- else
- fp = stdin; // 標準入力より入力
- */
- if((fpo=fopen(argv[2],"w"))==NULL)
- {
- printf("The file can't be opened. The program is exit.\n");
- return 0;
- }
- printf("\nsearch string: %s\n",schstr);
- //printf(" input file: %s\n",ifile);
- //--------------------------------------------------
- // 文字列の検索
- //--------------------------------------------------
- i = count = 0;
- memset(buf,0,sizeof(char)*512);
- while ( fgets(buf,512,fpi) )
- {
- //i=i+1;
- //chop_crlf(buf);
- p=strstr(buf,schstr);
- //p = search(buf,schstr);
- //printf("%d:%s\n",i,buf);
- //while ( p != NULL )
- if(p==NULL)
- {
- //printf("%d: %s\n",i,buf);
- fprintf(fpo,"%s\n",buf);
- }
- /*
- while ( p = NULL )
- {
- count ++;
- // printf("%d: %s [%s]\n",i+1,p,buf);
- printf("%d: %s\n",i,buf);
- //p = search(p+strlen(schstr),schstr);
- }
- */
- i++;
- memset(buf,0,sizeof(char)*512);
- }
- fclose(fpi);
- fclose(fpo);
- if ( !count )
- printf("string not found\n");
- return 0;
- }
- //-----------------------------------------------------------------
- // コマンドヘルプ
- //-----------------------------------------------------------------
- void usage(char* cmd)
- {
- fprintf(stderr,"usage: %s file -s <search string>\n",cmd);
- }
- //-----------------------------------------------------------------
- // 改行コードの除去
- //-----------------------------------------------------------------
- void chop_crlf(char* buff)
- {
- int i,bufleng = strlen(buff);
- for ( i=0; i<bufleng; i++ )
- {
- if ( buff[i] == '\r' ) buff[i] = 0;
- if ( buff[i] == '\n' ) buff[i] = 0;
- }
- }
- //-----------------------------------------------------------------
- // 文字列の検索
- //-----------------------------------------------------------------
- char* search(char* text, char* key)
- {
- int k,m,n;
- char *p;
- char skip[512];
- n = strlen(key);
- m = strlen(text);
- for ( k=0; k<512; k++ ) skip[k] = n;
- for ( k=0; k<n-1; k++ ) skip[(int)key[k]] = n-1-k;
- p = text+n-1;
- while ( p < text+m )
- {
- if ( *p == key[n-1] )
- { // 右端の文字だけ比較
- if ( strncmp(p-n+1,key,n) == 0 ) // キー全体を比較
- return (p-n+1);
- }
- p = p+skip[(int)*p]; // サーチ位置を進める
- }
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment