1.统计文件中的字符行数
std::string str;
std::fstream f;
f.open("b.txt",std::fstream::in|std::fstream::out) ;
long count=0;
while(std::getline(f,str))
{
count++;
}
2.统计一个文本文件的行数并加行号和空格
(统计一个文本文件的行数.输入文件的文件名作为命令行参数的传入,输出文件名为stdout,内容包括输入文件中的每一行文本,并且在每一行前加上行号和一个空格.)
#include<stdio.h>
main()
{FILE *fp,*fp1;
int cap=0, i=1;
char mid,filename[10];
printf("Input the filename like *.txt!/n");
scanf("%s",filename);
if((fp=fopen(filename,"r"))==NULL)
{printf("Can not open the file!/n");
exit (0);
}
if((fp1=fopen("stdout.txt","w+"))==NULL)
{printf("Can not open the file!/n");
exit (0);
}
while(!feof(fp))
{
mid=fgetc(fp);
if(mid=='/n') cap++;
}
fclose(fp);
if((fp=fopen(filename,"r"))==NULL)
{printf("Can not open the file!/n");
exit (0);
}
fprintf(fp1,"%d ",i++);
while(!feof(fp))
{
if(fputc(fgetc(fp),fp1)=='/n')
fprintf(fp1,"%d ",i++);
}
printf("cap=%d /n",cap+1);
fclose(fp);
fclose(fp1);
【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<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
【lightnut】:
#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;
system("pause");
return 0;
}
//测试文件test.txt内容:
abc abc abc lpte yejylyyryryljryjrabc
logyy[yuuujabcabc
aaaaaa aabeee aaa
//测试输出:
6 times of "abc" found in file: d:/temp/test.txt
5 times of "aaa" found in file: d:/temp/test.txt
【xuzheng318】:
#include <iostream>
#include <math.h>
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 <iostream>
using namespace std;
#include <fstream>
#include <string>
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;
}