SUGIYAMA

☆#IND_exit

Jan 14th, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define SCHSTR "#IND"
  6. #define std_in "<stdin>"
  7.  
  8. //-----------------------------------------------------------------
  9. void usage(char* cmd);
  10. void chop_crlf(char* buff);
  11. char* search(char* text, char* key);
  12. //-----------------------------------------------------------------
  13.  
  14. int main(int argc, char* argv[])
  15. {
  16.     FILE *fpi,*fpo;
  17.     int i,count;
  18.     char *cmd, *p;
  19.     const char* ifile = std_in;
  20.     char* schstr = SCHSTR;
  21.     char buf[512];
  22.     //--------------------------------------------------
  23.     // 引数の読込み
  24.     //--------------------------------------------------
  25.     /*
  26.     cmd = argv[0];
  27.     if ( argc<3 )
  28.     {
  29.         usage(cmd);
  30.         return 0;
  31.     }
  32.    
  33.     for ( i=1; i<argc; i++ )
  34.     {
  35.         if ( !strcmp(argv[i],"-s") )
  36.         {
  37.             if ( ++i >= argc )
  38.             {
  39.                 usage(cmd);
  40.                 return 0;
  41.             }
  42.             schstr = argv[i];
  43.         } else ifile = argv[i];
  44.     }
  45.      */
  46.    
  47.     if(argc!=3)
  48.     {
  49.         fprintf(stderr,"Usage: %s\n(1)Input_#IND_filename\n(2)Output_NON_#IND_filename\n",argv[0]);
  50.         exit(1);
  51.     }
  52.     if((fpi=fopen(argv[1],"r"))==NULL)
  53.     {
  54.         printf ("The file can't be opened. The program is exit.\n");
  55.        
  56.         return 0;
  57.     }
  58.  
  59.     /*
  60.     //--------------------------------------------------
  61.     // ファイル入力
  62.     //--------------------------------------------------
  63.     if ( strcmp(ifile,std_in) )
  64.     {
  65.         if ( ( fp = fopen(ifile,"r") ) == NULL )
  66.         {
  67.             fprintf(stderr,"No such file or directory.\n");
  68.             return -1;
  69.         }
  70.     }
  71.     else
  72.         fp = stdin; // 標準入力より入力
  73.     */
  74.    
  75.     if((fpo=fopen(argv[2],"w"))==NULL)
  76.     {
  77.         printf("The file can't be opened. The program is exit.\n");
  78.        
  79.         return 0;
  80.     }
  81.    
  82.    
  83.     printf("\nsearch string: %s\n",schstr);
  84.     //printf("   input file: %s\n",ifile);
  85.    
  86.    
  87.     //--------------------------------------------------
  88.     // 文字列の検索
  89.     //--------------------------------------------------
  90.     i = count = 0;
  91.     memset(buf,0,sizeof(char)*512);
  92.    
  93.     while ( fgets(buf,512,fpi) )
  94.     {
  95.         //i=i+1;
  96.         //chop_crlf(buf);
  97.         p=strstr(buf,schstr);
  98.         //p = search(buf,schstr);
  99.         //printf("%d:%s\n",i,buf);
  100.         //while ( p != NULL )
  101.         if(p==NULL)
  102.         {
  103.             //printf("%d: %s\n",i,buf);
  104.             fprintf(fpo,"%s\n",buf);
  105.         }
  106.        
  107.         /*
  108.         while ( p = NULL )
  109.         {
  110.             count ++;
  111.            // printf("%d: %s [%s]\n",i+1,p,buf);
  112.             printf("%d: %s\n",i,buf);
  113.             //p = search(p+strlen(schstr),schstr);
  114.         }
  115.          */
  116.         i++;
  117.         memset(buf,0,sizeof(char)*512);
  118.     }
  119.    
  120.     fclose(fpi);
  121.     fclose(fpo);
  122.     if ( !count )
  123.         printf("string not found\n");
  124.    
  125.     return 0;
  126. }
  127. //-----------------------------------------------------------------
  128. // コマンドヘルプ
  129. //-----------------------------------------------------------------
  130. void usage(char* cmd)
  131. {
  132.     fprintf(stderr,"usage: %s file -s <search string>\n",cmd);
  133. }
  134. //-----------------------------------------------------------------
  135. // 改行コードの除去
  136. //-----------------------------------------------------------------
  137. void chop_crlf(char* buff)
  138. {
  139.     int i,bufleng = strlen(buff);
  140.     for ( i=0; i<bufleng; i++ )
  141.     {
  142.         if ( buff[i] == '\r' ) buff[i] = 0;
  143.         if ( buff[i] == '\n' ) buff[i] = 0;
  144.     }
  145. }
  146. //-----------------------------------------------------------------
  147. // 文字列の検索
  148. //-----------------------------------------------------------------
  149. char* search(char* text, char* key)
  150. {
  151.     int k,m,n;
  152.     char *p;
  153.     char skip[512];
  154.     n = strlen(key);
  155.     m = strlen(text);
  156.     for ( k=0; k<512; k++ ) skip[k] = n;
  157.     for ( k=0; k<n-1; k++ ) skip[(int)key[k]] = n-1-k;
  158.     p = text+n-1;
  159.     while ( p < text+m )
  160.     {
  161.         if ( *p == key[n-1] )
  162.         {     // 右端の文字だけ比較
  163.             if ( strncmp(p-n+1,key,n) == 0 ) // キー全体を比較
  164.                 return (p-n+1);
  165.         }
  166.         p = p+skip[(int)*p];     // サーチ位置を進める
  167.     }
  168.     return NULL;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment