このコードは、テキストファイルのパターン発生数をカウントします。C++パターン発生数をカウントするスクリプト
#include <iostream>
int main()
{
// std::cout << "Hello World!" << std::endl;
// return 0;
ifstream fin("my_data.txt"); //opening text file
int count=0;
char ch[20],c[20];
cout<<"Enter a word to count:";
gets(c);
while(fin)
{
fin>>ch;
if(strcmp(ch,c)==0)
count++;
}
cout<<"Occurrence="<<count<<"n";
fin.close(); //closing file
return 0;
}
しかし、テスト時に、私はこのエラーを得た
10 2 C:Users \ユーザー\ 80977432 \ドキュメントは、C++ \ Untitled1.cppは、[エラー] 'はifstream' はこのスコープ
で宣言されていませんでした\します
のファイルI/O操作
#include<cstring>
ため#include <fstream>
を使用して使用します。 – songyuanyaohttp://en.cppreference.com/w/cpp/io/basic_ifstream/basic_ifstream – Danh