gpt4 book ai didi

c++ - 如何计算文本文件中的字符数

转载 作者:太空狗 更新时间:2023-10-29 23:41:29 26 4
gpt4 key购买 nike

我正在尝试用 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;
}

最佳答案

你在数行数。
你应该数一下字符。将其更改为:

while( getline ( inMyStream, line ) )
{
cout << line << endl;
c += line.length();
}

关于c++ - 如何计算文本文件中的字符数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10712117/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com