Chinaunix首页 | 论坛 | 博客
  • 博客访问: 251754
  • 博文数量: 49
  • 博客积分: 1231
  • 博客等级: 少尉
  • 技术积分: 967
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-02 00:04
个人简介

-->软硬件结合的系统级开发工程师,带过团队,爱好心理学,哲学,艺术...偶像:达芬奇

文章存档

2014年(2)

2013年(4)

2012年(15)

2011年(28)

分类: C/C++

2011-06-26 19:20:14

  1. /*=========================================
  2. 功能描述:查找文件中关键字
  3. 所用函数:无
  4. 返回值: 无
  5. =========================================*/

  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>

  9. #define TEXT 12
  10. #define BOX 80

  11. int main()
  12. {
  13.      FILE *fp;
  14.      char inputKey[TEXT];
  15.      char stringBox[BOX];
  16.      char ch;
  17.      int n=0;
  18.      int count=0;

  19.      printf("\n请输入要查询的关键字:");
  20.      scanf("%s",inputKey);
  21.      printf("\n你想要查询的关键字是: %s\n\n",inputKey);
  22.    
  23.      fp=fopen("file.txt","r");

  24.      do
  25.     {
  26.          ch=fgetc(fp);

  27.         if(ch!='\n')
  28.        {
  29.            stringBox[n++]=ch;
  30.        }
  31.        else
  32.        {
  33.            stringBox[n]='\0';

  34.            if(strstr(stringBox,inputKey))
  35.           {
  36.                printf("%s\n",stringBox);
  37.                count++;
  38.            }

  39.            n=0;
  40.         }
  41.      } while(ch!=EOF);

  42.      fclose(fp);

  43.      if(count!=0)
  44.          printf("\n共有[ %d ]条记录!\n",count);
  45.      else
  46.          printf("\n没有此记录!\n");

  47.      getch();

  48.      return 0;
  49. }
阅读(555) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~