69,379
社区成员
发帖
与我相关
我的任务
分享
/***********************************************************************
Copyright (c) 2009,liushac
All rights no reserved.
Name: <<C程序设计语言>>例程5-10
ID: hello.c
功能: 回应命令行变元,打印与第一个变元指定的模式串匹配的行。
Version: 0.1
Author: c.
Date: Sep 21, 2009
***********************************************************************/
#include <stdio.h>
#include <string.h>
#define MAXLINE 1000
int getline(char *line, int max);
int main(int argc, char *argv[ ])
{
char line[MAXLINE];
int found = 0;
if (argc != 2)
{
printf ("usage: hello.exe findme\n");
printf ("input:hello,world\n");
printf ("input:please findme\n");
printf ("display:please findme\n\n");
printf ("---------------------------\n");
}
else
{
while (getline(line, MAXLINE) > 0)
{
if (strstr(line, argv[1]) != NULL) /* 标准库函数strstr(s, t)用于返回子字符串t在字符串s中第一次出现处的指针;如果
子字符串t未在字符串s中出现过,那么返回值为空指针。它在头文件<string.h>中说明 */
{
printf ("---------------------------\n");
printf("ok.find \"%s\" of the line.\n%s\n", argv[1],line);
found++;
}
}
}
return found;
}
/* getline:将一行读入s中并返回其长度 */
int getline (char s[],int lim)
{
int c;
int i;
i=0;
while (i<lim-1 && (c=getchar())!=EOF && c!='\n'){
s[i]=c;
++i;
}
if (c=='\n'){
s[i]=c;
++i;
}
s[i]='\0';
return i;
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
system("PAUSE");
return 0;
}