- 論壇徽章:
- 0
|
- #include<stdio.h>
- #include<string.h>
- #define MAXLINE 10000
- int getline(char *line, int max);
- /*find函數(shù):打印與第一個(gè)參數(shù)指定的模式匹配的行*/
- main(int argc,int *argv[])
- {
- char line[MAXLINE];
- int found = 0;
- if(argc != 2)
- printf("Usage:find pattern\n");
- else
- {
- while(getline(line,MAXLINE) > 0)
- if(strstr(line,argv[1]) != NULL)
- {
- printf("%s",line);
- found++;
- }
- }
- return found;
- }
- /*getline:將輸入字符串存儲在s中,并返回輸入字符串的長度*/
- int getline(char s[],int lim)
- {
- int c,i;
- i = 0;
- while(--lim > 0 && (c = getchar() ) != EOF && c != '\n')
- s[i++] = c;
- if(c == '\n')
- s[i++] = c;
- s[i] = '\0';
- return i;
- }
復(fù)制代碼 用visual c++ 6.0編譯了這個(gè)程序后,在cmd命令窗口里輸入?yún)?shù)后,遇到的問題:
1,不管輸入幾個(gè)參數(shù),如 “D:\WIN-tc\chapter_5\Debug\5.10_test4 ab ehabc” ,輸出結(jié)果都是“Usage:find pattern”?
2,getline函數(shù)是“將輸入字符串存儲在s中,并返回此字符串的長度”,“輸入字符”和 后續(xù)的“命令行參數(shù)”是什么關(guān)系呢?感覺執(zhí)行階段輸入的(命令行)參數(shù)都保存在argv[]中了,沒有用到getline()函數(shù)?
在命令行參數(shù)的這個(gè)地方卡主了,先謝謝啦~ |
|