如何计算文本文件中的字符

use*_*910 3 c++ visual-c++

我试图用c ++来计算文本文件中的字符,这是我到目前为止,由于某种原因我得到4.即使你有123456个字符.如果我增加或减少角色我仍然得到4,请提前帮助和感谢

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const char FileName[] = "text.txt";

int main () 
{
string line;
ifstream inMyStream (FileName); 
int c;

if (inMyStream.is_open()) 
{

     while(  getline (inMyStream, line)){

             cout<<line<<endl;
              c++;
  }
    }
    inMyStream.close(); 

system("pause");
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

Bee*_*Guy 6

你在计算线条.
你应该统计人物.将其更改为:

while( getline ( inMyStream, line ) )
{
    cout << line << endl;
    c += line.length();
}
Run Code Online (Sandbox Code Playgroud)


sba*_*bbi 6

可能有数百种方法可以做到这一点。我认为最有效的是:

    inMyStream.seekg(0,std::ios_base::end);
    std::ios_base::streampos end_pos = inMyStream.tellg();

    return end_pos;
Run Code Online (Sandbox Code Playgroud)