请教一下,如何用C语言实现文件中关键字的查找和统计

kitty1044 2004-05-14 12:23:49
怎样用C语言实现在一个文本文件里查找几个关键字并统计每个的个数,文本里既有中文也有英文数字和其它字符,因为不知道如何兼顾总是找的个数不对,希望高手予以指点,如果有源代码可以学习就太好了.现在要急用,先行谢过!
...全文
791 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
summerxiaqing 2004-05-14
  • 打赏
  • 举报
回复
不好意思,上面的函数写错了应该是strstr(const char *s1, const char *s2);
改为如下代码:
#include <stdio.h>
#include <string.h>

int main(void)
{
FILE *file1;
char ch[MAX]; //MAX值自己定义,我用过好像10000都可以,在turbo c++3.0下
char source[] = "source"; //这是被查询指针,随便赋值的
char *search = ch;
int count = 0;

file1 = fopen("filename", "r");
fscanf("%s", ch);

search = strpstr(search, source);

while (search != NULL)
{
search = strstr(search, source);
search++;
count++;
}

fclose(file1);
//count就是个数,你要的函数可以自己改一下
return 0;
}
summerxiaqing 2004-05-14
  • 打赏
  • 举报
回复
不知道你要读的文件长不长,如果不长的话可以一次把文件内容读到一个字符缓冲区里,再调用strpbrk(const char *s1, const char *s2)(这个函数,这是定位s1中首次出现s2种字符串的函数,并将指针定位到该字符处。)然后将查询指针后移一位,在进行查找,直到strpbrk()返回NULL指针为止。以下是我写的一个简单代码。
#include <stdio.h>
#include <string.h>

int main(void)
{
FILE *file1;
char ch[MAX]; //MAX值自己定义,我用过好像10000都可以,在turbo c++3.0下
char source[] = "source"; //这是被查询指针,随便赋值的
char *search = ch;
int count = 0;

file1 = fopen("filename", "r");
fscanf("%s", ch);

search = strpbrk(search, source);

while (search != NULL)
{
search = strpbrk(search, source);
search++;
count++;
}

fclose(file1);
//count就是个数,你要的函数可以自己改一下
return 0;
}

//代码我没有编译过,所以不一定直接能用,^-^
cngdzhang 2004-05-14
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <conio.h>

FILE *f;

#define KEYNUM 17

char keyword[KEYNUM][10]={"void","char","int","long","double",
"for","do","while","if","else",
"switch","case","default","return","goto",
"break","continue"};
int keycount[KEYNUM];

void main()
{
char c;
char str[100];
char *p;
int i;
int total=0;

for(i=0;i<KEYNUM;i++)
{
keycount[i]=0;
}

clrscr();

f=fopen("asm.txt","rb");
while(!feof(f))
{
c=fgetc(f);
while(c==' ' || c=='\t' || c=='\r' || c=='\n') c=fgetc(f);
if(c==';')
{
while(c!='\n') c=fgetc(f);
}
if(isalpha(c))
{
p=str;
while(isalpha(c))
{
*p++=c;
c=fgetc(f);
}
*p='\0';
for(i=0;i<KEYNUM;i++)
{
if(strcmp(keyword[i],str)==0)
{
keycount[i]++;
total++;
break;
}
}
}
}
printf("Total KeyWords : %d\n\n",total);
for(i=0;i<KEYNUM;i++)
{
printf("%-10s %3d\n",keyword[i],keycount[i]);
}
fclose(f);
}
sharkhuang 2004-05-14
  • 打赏
  • 举报
回复
strstr


strtok
heibai520 2004-05-14
  • 打赏
  • 举报
回复
可以这样实现!
我试了!

#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<conio.h>
struct KeyWord{
char Keyword[20];
int count;
};

KeyWord KeywordTable[]=
{
{"else",0},{"for",0},{"if",0},{"include",0},{"while",0}
};

int SeqSearch(KeyWord *tab,int n,char *word)
{
int i;
for(i=0;i<n;i++,tab++)
if(strcmp(word,tab->Keyword)==0)
return i;
return -1;
}

int Getword(ifstream &fin,char w[])
{
char c;
int i=0;
while(fin.get(c)&&!isalpha(c));
if(fin.eof())
return 0;
w[i++]=c;
while(fin.get(c)&&(isalpha(c)||isdigit(c)))
w[i++]=c;
w[i]='\0';
return 1;
}

void main(void)
{
const int MAXWORD=50;
const int NKEYWORDS=sizeof(KeywordTable)/sizeof(KeyWord);
int n;
char word[MAXWORD],c;
ifstream fin;
fin.open("prg2_5.cpp",ios::in | ios::nocreate);/prg2_5.cpp就是统计本文件
if(!fin)
{
cerr<<"Could not open file'prg2_5.cpp'"<<endl;
exit(1);
}
while(Getword(fin,word))
if((n=SeqSearch(KeywordTable,NKEYWORDS,word))!=-1)
KeywordTable[n].count++;
for(n=0;n<NKEYWORDS;n++)
if(KeywordTable[n].count>0)
{
cout<<KeywordTable[n].count;
cout<<" "<<KeywordTable[n].Keyword<<endl;
}
fin.close();
getch();
}
BluntBlade 2004-05-14
  • 打赏
  • 举报
回复
可以考虑使用Boost库里的Regex类(正则表达式),功能强大,方便易用。
kitty1044 2004-05-14
  • 打赏
  • 举报
回复
多谢各位好心人,我要做的工作是在网页里的文本文件里搜索我输入的字符串(也就是关键字),网页里的文本应该大一些,一次读到字符缓冲区里也可以吗?如果我要查询多个词是不是可以把这些词放到一个字符串数组里依次查找?

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧