0

我正在使用 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:在左大括号“{”之前找到文件结尾

谢谢!

4

3 回答 3

3

这通常意味着大括号没有闭合。在您的情况下,while 循环没有用花括号关闭。我也认为你没有关闭 if 语句。

于 2011-03-30T06:19:23.393 回答
2

你错过了两个右括号。我认为您的代码应以以下行结尾:

            cout<<"the number of times the word occured in the file is "<<cnt<<endl;
        }
        index.close();
    }
    system("pause");
    return 0;
}
于 2011-03-30T06:25:22.217 回答
1

cout<<"文件中单词出现的次数是"<

像这样

} index.close();

于 2011-03-30T07:05:44.487 回答