c++ 文本文件中查找字符串

在一个已知的文本文件(是文章)中查找字符串,统计该字符串在文章中出现次数,字符串由用户给定
代码尽量详细
谢谢!
谢谢啊看来我没说清楚……最好用上<fstream>库和类
要不我也会C的……但到c++就晕了……
最新回答
劫后余生

2024-05-16 13:40:25

我觉得是查找一篇文章中某个单词数出现的次数,但是你题目的要求是查找字符串出现的次数,所以我还是按你的题意来写的

还有,我并不赞同楼上那些用C风格字符串的处理方式,C不像C,C++不像C++。
C++的字符串处理远比C要简单便捷

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
fstream fin("file.txt", ios::in);
if(!fin)
{
cerr << "cannot open file " << endl;
return -1;
}
string s;
cout << "请输入要查找的字符串:";
cin >> s;
string t;
int num = 0;
string::size_type i;
while(!fin.eof())
{
fin >> t;
if(t.size() > s.size())
{
for(i = 0; i != s.size(); i++)
{
if(t[i] != s[i])
break;
}
if(i == s.size())
num++;
}
else if (t == s)
num++;
}
fin.close();
cout << "该字符串共出现了" << num << "次" << endl;
return 0;
}
笙歌白云上

2024-05-16 10:52:39

100分,加给我
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char token; // 当前从文件获取的字符
int count;

void parse(char token, FILE *fp, char wordname[]);
void cmpword(char str[], char name[]);

int main()
{
// 输入文件名,可以包含路径,如果与程序在相同上当,可只写文件名不写目录
char fname[50]; //文件名
char wordname[100]; //要查找的字符串
FILE *fp;
count = 0;
printf("please input file name: ");
scanf("%s", fname);
printf("please input word name: ");
scanf("%s", wordname);
fp = fopen(fname, "r");
if (fp == NULL)
{
printf("IO ERROR\n"); // 文件打开出错
printf("press any key to exit");
return 0;
}
else
{
while ((token = fgetc(fp)) != EOF)
{
switch (token)
{
// 空白符和换行的处理
case ' ':
case '\t':
case '\n':
break;
// 关键词的处理
default:
parse(token, fp, wordname);
break;
}
}
}
printf("%s appear %d times!\n", wordname, count);
return 0;
}

// 关键词分析程序
void parse(char token, FILE *fp, char wordname[])
{
char keyword[1024];
int index = 0;
// 将每个词第一个字符赋给数组第一个元素
keyword[0] = token;
// 开始读字符,直到遇到空白符,说明找到一个词
while ((keyword[++index] = fgetc(fp)) != ' ' &&
keyword[index] != '\t' && keyword[index] != EOF &&
keyword[index] != '\n')
;
// 加结束符
keyword[index] = '\0';
//比较是否为要查找的关键词
cmpword(keyword, wordname);
}

//查找计数
void cmpword(char str[], char name[])
{
if (strcmp(str, name) == 0)
count++;
}
爱你这出戏请给我一些台词

2024-05-16 13:54:18

==========================================
问题补充:谢谢啊看来我没说清楚……最好用上<fstream>库和类
要不我也会C的……但到c++就晕了……
==========================================
给,下面是用上<fstream>库的程序,已经编译运行确认,你再看看吧:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
using namespace std;

#define MAXLINELENGTH 1024
#define N 20 //暂定需要查找的字符串的最大长度为20

int sumstrstr(char *, char *);

int main()
{
char str[N]={NULL};
char txtarray[MAXLINELENGTH];
int i = 0;
char c;
int sumcount = 0;
fstream fp;

fp.open("data.txt",ios::in); //暂定文件名为"data.txt"

memset(txtarray, 0, sizeof(txtarray));

if (!fp)
{
cout<<"Open File Error..."<<endl;
exit(1);
}

cout<<"请输入要查找的字符串: "<<endl;
cin>>str;

while (fp>>c)
{
if(c== EOF) break;

if (c != '\n')
{
txtarray[i] = c;
i++;
}
else//Read a line of the file

{
sumcount += sumstrstr(txtarray, str);
i = 0;
memset(txtarray, 0, sizeof(txtarray));
}
}

sumcount += sumstrstr(txtarray, str);//The last line of the file

cout<<endl<<"该字符串在文章中出现次数为: "<<sumcount<<"次."<<endl;

fp.close();;

system("pause");
return 0;
}

int sumstrstr(char *str1, char *str2)
{
int sum = 0;
char *str3 = NULL;
int i = 0;
while ((str3 = strstr(str1, str2)) != NULL)
{
str1 = str3;
sum++;
for (i = 0; i < strlen(str2); i++)
str1++;
}

return sum;
}
候补的爱人

2024-05-16 02:35:47

这里有各种算法综述的文章,你看看。
http://www.yuanma.org/data/2008/0806/article_3128.htm


你喜欢那个算法,就找那个算法的源码。应该很简单。自己写一个也行,就是估计效率比较差。
浮世三月

2024-05-16 11:34:08

问老师