我用一个CString的变量去读取了 TXT的内容  那么如何计算行数呢
                               谢谢

解决方案 »

  1.   

    INT i = -1;
    INT c = 0;
    do 
    {
    i = s.Find(_T('\n'), i + 1);
    ++c;
    } while (i != -1 && i != s.GetLength() - 1);
      

  2.   

    更土一点的办法,循环 CStdioFile.ReadString
    int nCount = 0;
    while (CStdioFile.ReadString(...))
    {
        nCount++;
    }
    但是效率上就没有直接查找 "\n" 来的快了。
    只是作为一种思路。
      

  3.   


    有个问题,可能会漏掉最后一行,
    CString strText(_T(""));
    strText="....";//保存你的文件内容
    int flag=-1;
    int nCount=0;
    while(-1!=(flag=strText.Find("\r\n"))
    {
      nCount++;
      strText=strText.Right(strText.GetLength()-flag-2);
    }
    if(!strText.IsEmpty()) //最后一行内容
    {
      nCount++;
    }CString str(_T(""));
    str.Format(_T("%d"),nCount);
    AfxMessageBox(str);
      

  4.   

    对于上的strText,可以先过滤某一些字符,如strText.Trim(_T(" \r\n"));