Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 获取流定位c++;_C++ - Fatal编程技术网

C++ 获取流定位c++;

C++ 获取流定位c++;,c++,C++,我有以下任务 我需要检查我正在打开的文件中有多少字符、单词和行。所以我来解决这个问题 当我需要定位get指针时,同样在开始时,它不起作用。虽然((getline(f,charCount)),但它刚刚去世,没有进入 代码如下: void count(ifstream &f) { int countChar = 0, countWord = 0, countRow = 0; string charCount; string wordCount; while (

我有以下任务

我需要检查我正在打开的文件中有多少字符、单词和行。所以我来解决这个问题

当我需要定位get指针时,同样在开始时,它不起作用。
虽然((getline(f,charCount))
,但它刚刚去世,没有进入

代码如下:

void count(ifstream &f)
{
    int countChar = 0, countWord = 0, countRow = 0;
    string charCount;
    string wordCount;
    while (!f.eof())
    {
        f >> wordCount;
        countWord++;

    }
    cout << " Words : " << countWord << endl;
    f.seekg(ios::beg);
    while (getline(f, charCount))
    {
        countChar += charCount.length();
        countRow++;
    }

    cout << "Characters: " << countChar << endl;
    cout << "Rows : " << countRow << endl;
}
无效计数(ifstream&f)
{
int countChar=0,countWord=0,countRow=0;
字符串字符数;
字符串字数;
而(!f.eof())
{
f>>字数;
countWord++;
}
不能使用

这是错误的

应该是

while (f >> wordCount)
{
    countWord++;
}
进一步阅读:

一旦达到EOF,您需要清除流的错误标志,然后才能再次使用它

f.clear();
f.seekg(ios::beg);
while (getline(f, charCount)) ...
.
f.clear();
f.seekg(ios::beg);
while (getline(f, charCount)) ...