加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

怎样指定文件中统计一个字符串出现的次数(C/C++)

(2013-02-27 16:04:50)
分类: C/C
求助各位大虾:怎样指定文件中查找一个字符串并显示该字符串出现的次数,比如说在D:\tools\abc.txt文件中寻找bbc字符串出现的次数 。最好帮忙写个完整的程序,谢谢

http://www.programfan.com/club/image/arrow3.gif 怎样指定文件中统计一个字符串出现的次数(C/C++)

求助各位大虾:怎样指定文件中查找一个字符串并显示该字符串出现的次数,比如说在D:\tools\abc.txt文件中寻找bbc字符串出现的次数 。最好帮忙写个完整的程序,谢谢

http://www.programfan.com/club/image/arrow3.gif 回复内容
【XyRbj】:
用正则表达式去查找 这是最好的办法。

【todototry】:
统计文件的单词数
读文件的一行


int main()
{
 ifstream infile;
 string filename;
 cout << "Please enter the file name: ";
 cin >> filename;

 infile.open(filename.c_str());
 string line;
 getline(infile, line, '\n');
 infile.close();

 vector wordsOfLine;
 string::size_type pos 0, prev_pos =0;
 string word;
 while ((pos line.find_first_of(' ', pos)) != string::npos)
 {
  word line.substr(prev_pos, pos prev_pos);
  prev_pos ++pos;
  wordsOfLine.push_back(word);
 }
 wordsOfLine.push_back(line.substr(prev_pos, pos prev_pos));

 size_t numOfLine wordsOfLine.size();
 cout << numOfLine << "words" << endl;
}
读整个文件的:

int main()
{
    ifstream infile;
    string filename;
    cout << "Please enter the file name: ";
    cin >> filename;

    infile.open(filename.c_str());
    string line;
    vector wordsOfFile;
    while (getline(infile, line, '\n'))
    {
        string::size_type pos 0, prev_pos =0;
        string word;
        while ((pos line.find_first_of(' ', pos)) != string::npos)
        {
            word line.substr(prev_pos, pos prev_pos);
            prev_pos ++pos;
            wordsOfFile.push_back(word);
        }
        wordsOfFile.push_back(line.substr(prev_pos, pos prev_pos));
    }

    infile.close();

    size_t numOfLine wordsOfFile.size();
    cout << numOfLine << "words" << endl;

    return 0;
}

加上判断即可,看着用吧,呵呵^_^okokok

【lightnut】:
#include 
#include 
#include 

using namespace std;

int CountSubString(string const& str, string const& substr)
{
   int nCount 0;

   string::size_type substrSize substr.size();
   string::size_type idxSub str.find(substr, 0);
   while (idxSub!=string::npos) {
      ++nCount;
      ++idxSub;
      idxSub str.find(substr, idxSub);      
   }

   return nCount;
}

int CountStrInFile(string const& filename,  string const& str)
{
   ifstream inf(filename.c_str()); 
   if (!inf) {
      cout<<"Error: can't open the file: "<<filename<<endl;
      exit(1);
   }

   string infStr;
   int nSubStrFound 0;
   while (inf && !inf.eof()){
      inf>>infStr;
      nSubStrFound += CountSubString(infStr, str);      
   }
    
   return nSubStrFound;
}



int main()
{
   string filename("d:\\temp\\test.txt");   // the file name to search string
   string strToCount("abc");                // the string to count
   
   int nCount CountStrInFile(filename, strToCount);
   cout<<nCount<<" times of \""<<strToCount<<"\" found in file: "<<filename<<endl;  

   strToCount "aaa";
   nCount CountStrInFile(filename, strToCount);
   cout<<nCount<<" times of \""<<strToCount<<"\" found in file: "<<filename<<endl;  

   system("pause");
   return 0;
}

//测试文件test.txt内容:
abc abc abc lpte yejylyyryryljryjrabc
logyy[yuuujabcabc
aaaaaa aabeee aaa

//测试输出:
times of "abc" found in file: d:\temp\test.txt
times of "aaa" found in file: d:\temp\test.txt


【xuzheng318】:
#include  
#include  

using namespace std; 

int main() 

char str1[255],str2[255],*p1,*p2, *temp; 
int sum=0; 
cout<<"intput two strings"<<endl; 
cin>>str1; 
cin>>str2; 
p1=str1; 
p2=str2; 

while (*p1!='\0') 

temp p1; 
if(*temp==*p2) 


while((*temp==*p2)&&(*p2!='\0')&&(*temp!='\0')) 

temp++; 
p2++; 


p1++; 
if(*p2=='\0') sum=sum+1; 
p2=str2; 

cout<<sum; 
return 0; 

}


【crypticjade】:
谢谢各位了

【hamlet0168】:
#include 
using namespace std;
#include 
#include 

int WordCount_Of_File(const string filename const string word);

void main()
{
    cout<<WordCount_Of_File("word.txt","er")<<endl;
system("pause");
}

//在指定文件中查找单词,并返回找到的单词总数
int WordCount_Of_File(const string filename const string word)
{
//打开文本文件以便读入
ifstream infile(filename.c_str (),ios::in);
if(!infile)
{
   cerr<<"unable to open file"<<filename<<"--bailing out!\n";
   ::system ("pause");
   return 0;
}
//单词总数
int iCount=0;
//单行文本
string text_line ;
//依次读取每一行,这也以为着,将来无法
//使用此函数来查找诸如 "a\nb"字样的单词
//因为我把它截断了
while(getline(infile,text_line,'\n'))
{
//为了方面检查结果,我把每一行的内容输出了,可去掉
cout<<text_line<<endl;
string::size_type pos=0;//记录找到单词的位置
while((pos=text_line.find(word,pos))!=string::npos)
{
//找到单词,单词总数+1
iCount++;
//这里用的是++pos,而不是pos+=word.size()
//比如在字符"aaaaa"查找"aaa",答案是1还是3的问题
//我认为应该是3,所以++pos;
++pos;
}
}
return iCount;
}

 

 

【todototry】:
统计文件的单词数
读文件的一行


int main()
{
 ifstream infile;
 string filename;
 cout << "Please enter the file name: ";
 cin >> filename;

 infile.open(filename.c_str());
 string line;
 getline(infile, line, '\n');
 infile.close();

 vector wordsOfLine;
 string::size_type pos 0, prev_pos =0;
 string word;
 while ((pos line.find_first_of(' ', pos)) != string::npos)
 {
  word line.substr(prev_pos, pos prev_pos);
  prev_pos ++pos;
  wordsOfLine.push_back(word);
 }
 wordsOfLine.push_back(line.substr(prev_pos, pos prev_pos));

 size_t numOfLine wordsOfLine.size();
 cout << numOfLine << "words" << endl;
}
读整个文件的:

int main()
{
    ifstream infile;
    string filename;
    cout << "Please enter the file name: ";
    cin >> filename;

    infile.open(filename.c_str());
    string line;
    vector wordsOfFile;
    while (getline(infile, line, '\n'))
    {
        string::size_type pos 0, prev_pos =0;
        string word;
        while ((pos line.find_first_of(' ', pos)) != string::npos)
        {
            word line.substr(prev_pos, pos prev_pos);
            prev_pos ++pos;
            wordsOfFile.push_back(word);
        }
        wordsOfFile.push_back(line.substr(prev_pos, pos prev_pos));
    }

    infile.close();

    size_t numOfLine wordsOfFile.size();
    cout << numOfLine << "words" << endl;

    return 0;
}

加上判断即可,看着用吧,呵呵^_^okokok

【lightnut】:
#include 
#include 
#include 

using namespace std;

int CountSubString(string const& str, string const& substr)
{
   int nCount 0;

   string::size_type substrSize substr.size();
   string::size_type idxSub str.find(substr, 0);
   while (idxSub!=string::npos) {
      ++nCount;
      ++idxSub;
      idxSub str.find(substr, idxSub);      
   }

   return nCount;
}

int CountStrInFile(string const& filename,  string const& str)
{
   ifstream inf(filename.c_str()); 
   if (!inf) {
      cout<<"Error: can't open the file: "<<filename<<endl;
      exit(1);
   }

   string infStr;
   int nSubStrFound 0;
   while (inf && !inf.eof()){
      inf>>infStr;
      nSubStrFound += CountSubString(infStr, str);      
   }
    
   return nSubStrFound;
}



int main()
{
   string filename("d:\\temp\\test.txt");   // the file name to search string
   string strToCount("abc");                // the string to count
   
   int nCount CountStrInFile(filename, strToCount);
   cout<<nCount<<" times of \""<<strToCount<<"\" found in file: "<<filename<<endl;  

   strToCount "aaa";
   nCount CountStrInFile(filename, strToCount);
   cout<<nCount<<" times of \""<<strToCount<<"\" found in file: "<<filename<<endl;  

   system("pause");
   return 0;
}

//测试文件test.txt内容:
abc abc abc lpte yejylyyryryljryjrabc
logyy[yuuujabcabc
aaaaaa aabeee aaa

//测试输出:
times of "abc" found in file: d:\temp\test.txt
times of "aaa" found in file: d:\temp\test.txt


【xuzheng318】:
#include  
#include  

using namespace std; 

int main() 

char str1[255],str2[255],*p1,*p2, *temp; 
int sum=0; 
cout<<"intput two strings"<<endl; 
cin>>str1; 
cin>>str2; 
p1=str1; 
p2=str2; 

while (*p1!='\0') 

temp p1; 
if(*temp==*p2) 


while((*temp==*p2)&&(*p2!='\0')&&(*temp!='\0')) 

temp++; 
p2++; 


p1++; 
if(*p2=='\0') sum=sum+1; 
p2=str2; 

cout<<sum; 
return 0; 

}


【crypticjade】:
谢谢各位了

【hamlet0168】:
#include 
using namespace std;
#include 
#include 

int WordCount_Of_File(const string filename const string word);

void main()
{
    cout<<WordCount_Of_File("word.txt","er")<<endl;
system("pause");
}

//在指定文件中查找单词,并返回找到的单词总数
int WordCount_Of_File(const string filename const string word)
{
//打开文本文件以便读入
ifstream infile(filename.c_str (),ios::in);
if(!infile)
{
   cerr<<"unable to open file"<<filename<<"--bailing out!\n";
   ::system ("pause");
   return 0;
}
//单词总数
int iCount=0;
//单行文本
string text_line ;
//依次读取每一行,这也以为着,将来无法
//使用此函数来查找诸如 "a\nb"字样的单词
//因为我把它截断了
while(getline(infile,text_line,'\n'))
{
//为了方面检查结果,我把每一行的内容输出了,可去掉
cout<<text_line<<endl;
string::size_type pos=0;//记录找到单词的位置
while((pos=text_line.find(word,pos))!=string::npos)
{
//找到单词,单词总数+1
iCount++;
//这里用的是++pos,而不是pos+=word.size()
//比如在字符"aaaaa"查找"aaa",答案是1还是3的问题
//我认为应该是3,所以++pos;
++pos;
}
}
return iCount;

0

阅读 收藏 喜欢 打印举报/Report
前一篇:vector
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有