簡體   English   中英

第5.10節來自Kernighan / Ritchie命令行參數/可選參數

[英]Section 5.10 from Kernighan/Ritchie Command line arguments/optional parameters

第一次海報。 希望有人可以幫助我。 在5.10節中,Kernighan給出了該程序的示例,該程序重新打印帶有字符串的文本行。 因此我將其保存為“ find”在我的文件夾中,然后進入cmd,然后進入該文件夾,然后鍵入find“ -x what”。 但由於某些原因,' - '沒有注冊,它只是將“-x whatever”視為一個長字符串。 任何人都知道為什么會這樣嗎? 謝謝。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINE 1000

int getline(char *s, int lim)
{
int i = 0;

while(i < lim - 1 && (*s = getchar()) != EOF && *s++ != '\n')
   i++;

if(*s == '\n')
  *s++ = '\n', i++;

*s = '\0';
return i;
}
int main(int argc, char *argv[])
{

 char line[MAXLINE];
 long lineno = 0;
 int c, except = 0, number = 0, found = 0;

 while(--argc > 0 && (*++argv)[0] == '-')
    while(c = *++argv[0])
      switch(c) {
         case 'x':
              except = 1;
              break;
         case 'n':
              number = 1;
              break;
         default:
              printf("find: illegal option %c\n", c);
              argc = 0;
              found = -1;
              break;
      }

 if(argc != 1)
     printf("Usage: find -x -n pattern\n");
 else
     while(getline(line, MAXLINE) > 0) {
         lineno++;
         if((strstr(line, *argv) != NULL) != except) {
               if(number)
                  printf("%ld:", lineno);
               printf("%s", line);
               found++;
         }
     }

 printf("Found: %d", found);
 return found;
}

你必須打字

  find -x whatever

代替

  find "-x whatever"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM