统计某文件出现某字符串的次数,该如何处理
统计某文件出现某字符串的次数
//统计某文件出现某字符串的次数
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
using namespace std;
void main()
{
char file[] = "file.txt";
ifstream fin(file);
if(!fin)
{
cout << "文件打开失败!";
return;
}
char Str[] = "str";
int lenStr = strlen(Str);
int Count = 0;
char Word[50];
while(fin >> setw(50) >> Word)
{
char *NewWord = Word;
char *p = strstr(NewWord, Str);
while(NULL != p)
{
Count++;
NewWord = p + lenStr;
p = strstr(NewWord, Str);
}
}
fin.close();
cout << "文件" << file << "总共出现" << Count << "个" << Str << endl;
}
setw(50)在程序中有何用处。。。怎样发挥作用????求解释!!!!!!
------解决方案--------------------
http://hi.baidu.com/golny/blog/item/06eb532572417a7834a80ffb.html
------解决方案--------------------
楼主,你用setw想达到 什么 效果?
------解决方案--------------------
//统计某文件出现某字符串的次数
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
using namespace std;
void main()
{
char file[] = "file.txt";
ifstream fin(file);
if(!fin)
{
cout << "文件打开失败!";
return;
}
char Str[] = "str";
int lenStr = strlen(Str);
int Count = 0;
char Word[50];
while(fin >> setw(50) >> Word)
{
char *NewWord = Word;
char *p = strstr(NewWord, Str);
while(NULL != p)
{
Count++;
NewWord = p + lenStr;
p = strstr(NewWord, Str);
}
}
fin.close();
cout << "文件" << file << "总共出现" << Count << "个" << Str << endl;
}
setw(50)在程序中有何用处。。。怎样发挥作用????求解释!!!!!!
------解决方案--------------------
http://hi.baidu.com/golny/blog/item/06eb532572417a7834a80ffb.html
------解决方案--------------------
楼主,你用setw想达到 什么 效果?
------解决方案--------------------