我正在使用 C++ 处理文件,但遇到一个无法删除的奇怪错误。我对文件处理非常陌生,所以请帮帮我。
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(void)
{
fstream index;
index.open("file1",ios::app);
index<<"this is the indexed file"<<endl;
index<<"file name /t"<<"size /t"<<"path"<<endl;
//index.close();
string line;
string wordtofind;
char filename[50];
int cnt;
//ifstream index("file1.txt");
if(index.is_open())
{
while(!index.eof())
{
getline(index,line);
cout<<line<<endl;
cout<<"enter a word to be searched"<<endl;
cin>>wordtofind;
cout<<"enter the file in which you want to search the given word"<<endl;
cin>>filename;
//cnt=count(filename,wordtofind);
int counter=0;
ifstream file;
string word;
file.open(filename);
if(!file) //If not exist
{
cout << "Could not open file" << endl;
}
else
{
while(file >> word)
{
if(word.compare(wordtofind) == 0)
counter++;
}
}
file.close(); //always pays to be tidy
cout<<"the number of times the word occured in the file is "<<cnt<<endl;
index.close();
system("pause");
return 0;
}
错误:
致命错误 C1075:在左大括号“{”之前找到文件结尾
谢谢!