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