c/c++對文件統計函數

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;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章