Chinaunix首页 | 论坛 | 博客
  • 博客访问: 209718
  • 博文数量: 136
  • 博客积分: 2919
  • 博客等级: 少校
  • 技术积分: 1299
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-11 09:08
文章分类

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: C/C++

2011-03-23 09:04:32

  1. /* k&r(5.10): Command-line Arguments
  2.    created on Mar 23, 2011
  3.    */
  4. #include "stdio.h"
  5. #include "string.h"
  6. #define MAXLINE 1000

  7. int getline1(char *line, int max);

  8. /* find: print lines that match pattern from 1st arg */
  9. main(int argc, char *argv[])
  10. {
  11.     char line[MAXLINE];
  12.     int found = 0;

  13.     if (argc != 2)
  14.      printf("Usage: find pattern\n");
  15.     else
  16.      while (getline1(line, MAXLINE) > 0)
  17.         /* strstr(s,t): return a pointer to the first occurrence
  18.          of the string t in the string s, or NULL if there is none. */
  19.         if (strstr(line, argv[1]) != NULL) {
  20.             printf("%s", line);
  21.             found++;
  22.         }
  23.     return found;
  24. }

  25. /* getline: return the number of lines of input */
  26. int getline1(char *line, int max)
  27. {
  28.     int i, c;
  29.     i = 0;

  30.     for (i = 0; i < max-1 && (c=getchar()) != EOF && c != '\n'; i++)
  31.      line[i] = c;
  32.     if (c = '\n')
  33.      line[i++] = c;
  34.     line[i] = '\0';
  35.     return i;
  36. }
阅读(490) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~