如何统计一个文本文件里一个特定字符串出现了多少次?请给出实例代码,谢谢谢谢!!!

解决方案 »

  1.   

    int count=0;
    while(文件没有读完)
    {
      if(readed==xxx)
        count++;
    }
      

  2.   

    to nightsuns(nightsuns) : 我要计算的是包含的字符串,并不是比较单个字符阿
      

  3.   

    while(未完)
    {
       读一段
       strcmp
    }
      

  4.   

    告诉你思路吧,以前写过这样的代码.int nLength = Strlen("YourString");
    int nNumsStrings = 0;
    int nNumCompare = 0;
    从文件头开始,找到第一个跟字符串相等的字符,
       nNumCompare ++;
    比较第二个;还是相等 nNumCompare ++;
    如果nNumCompare ==nLength 或者 出现不等了
    {
       说明没有找到;
       nNumCompare  = 0;
    }else
    {
       nNumsStrings ++;
       nNumCompare =0;
    }大致思路肯定是正确的,至于循环你自己考虑,应该很简单的.
       
        
      

  5.   

    写错了.
    if(nNumCompare ==nLength)
    {
       // 找到
       nNumsStrings ++;
       nNumCompare =0;
    }else
    {
       if(出现不等的)
       {
            // 没有找到,重新标记
            ...
        }

       
      

  6.   

    to unrise(小超) : 那个while循环怎么写?一次往后面跳一个字符
      

  7.   

    用CString的简单,先把你的文件整个读入一个CString body中
    i = body.find("yourstring");
    while(i!=-1)
    {
        body = body.right(body.getlength()-i);
        i = body.find("yourstring");
        index++;
    }
      

  8.   

    #include "string.h" 
             CFile file;
    file.Open("FileName",CFile::modeRead | CFile::typeBinary);
    int length = file.GetLength();
    VERIFY(length > 0);
    char *pStrBuffer = new char[length];// source string
    file.Read(pStrBuffer,length);
    file.Close();
    char str2find[] = "ok";// string to find
    char *pDest = NULL;
    int count = 0, pos = 0;
    while(pos < length)
    {
    pDest = strstr(pStrBuffer + pos,str2find);
    if(pDest == NULL) 
    break;
    else
    {
    pos = pDest - pStrBuffer + 1;
    count++;//这是字符串出现的次数
    }
    }
    delete pStrBuffer;
      

  9.   

    to photoman() : 文件有几十兆呢,没办法读到内存里 :(to Anikan(皮皮鱼) : 你的程序有未知错误 :( 5555我想这样:先获取一个字符串长度,然后从文件中第一个字符开始,每次读取这个长度的字符串,比较后从第二个字符读字符串,不知道这样的循环该怎么写?
      

  10.   

    Anikan(皮皮鱼) ( ) 方法可行。
      

  11.   

    程序:
    #include <iostream>
    #include <fstream>
    #include <string>using namespace std;
    const std::string Key = "wo";int main(int argc, char* argv[])
    {
      if (argc >= 2)
      {
        std::ifstream ifile(argv[1]);
        if (ifile.is_open())
        {
          int n = 0; 
          std::string str;
          while(ifile >> str)
          {
             for(int k=0; (k=str.find(Key,k))!=std::string::npos; k++)
    {
      n++;
    }
          }
          std::cout << "关键字有 : " << n << std::endl;
         }
        }
       else
       {
          std::cout << "No file for open !" << endl;
        }
      return 0;
    }
    测试文件:
    wo, wo, wo, and to bo ok wwe wo 
    mm oo m wo wowo and wowowk wookwokwo.
    wwo kkwo kkwkwkowo搜索关键字: wo输出:15
      

  12.   

    你编译测试一下,不行告诉我。
    用MFC和C++的思路一样。可以自己写一下,我也可以帮你写。
      

  13.   

    不行,standard c 的我不太熟悉,你能帮我用mfc改写出一个函数吗?谢谢了~~ 我是在搞不定了
      

  14.   

    而且用CString你一次读几M不会有问题
      

  15.   

    CFileDialog dlg(TRUE);
    if (dlg.DoModal() == IDOK)
    {
    CString strFile = dlg.GetPathName();
    CStdioFile file(strFile, CFile::modeRead);
    CString strTxt;
    CString strKey = "wo";
    int n = 0;
    while (file.ReadString(strTxt))
    {
    for(int k=0; (k=strTxt.Find(strKey, k))!=-1; k++)
    {
    n++;
    }
    }
    TRACE("***** Wo Count : %d", n);
    }
      

  16.   

    测试文件:
    wo, wo, wo, and to bo ok wwe wo 
    mm oo m wo wowo and wowowk wookwokwo.
    wwo kkwo kkwkwkowo搜索关键字: wo输出:15主要代码:
    CStdioFile file(strFile, CFile::modeRead);
    CString strTxt;
    CString strKey = "wo"; //关键字
    int n = 0;   //统计个数while (file.ReadString(strTxt))
    {
         for(int k=0; (k=strTxt.Find(strKey, k))!=-1; k++)
         {
    n++;
         }
    }
      

  17.   

    通过了吗?不行话留个e-mail我把程序发过去。
      

  18.   

    搞定了!谢谢大家! SoLike 我再开帖给你分~~!!