怎样指定文件中统计一个字符串出现的次数(C/C++),该怎么处理
怎样指定文件中统计一个字符串出现的次数(C/C++)
求助各位大虾:怎样指定文件中查找一个字符串并显示该字符串出现的次数,比如说在D:\tools\abc.txt文件中寻找bbc字符串出现的次数 。最好帮忙写个完整的程序,谢谢
------解决方案--------------------
统计文件的单词数
读文件的一行
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 <string> 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 <string> 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
------解决方案--------------------
#include <iostream>
#include <fstream>
#include <string>
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;
求助各位大虾:怎样指定文件中查找一个字符串并显示该字符串出现的次数,比如说在D:\tools\abc.txt文件中寻找bbc字符串出现的次数 。最好帮忙写个完整的程序,谢谢
------解决方案--------------------
统计文件的单词数
读文件的一行
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 <string> 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 <string> 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
------解决方案--------------------
#include <iostream>
#include <fstream>
#include <string>
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;