【问题标题】:Fatal Error While Working On Files处理文件时出现致命错误
【发布时间】:2011-03-30 06:15:11
【问题描述】:

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

之前

谢谢!

【问题讨论】:

  • 你可以t read and write file with one instance of fstream. here: fstream index; indexindex 并重新打开带有读取标志的文件 '"file1"',当您想从中读取时

标签: c++ file file-handling


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

                cout<<"the number of times the word occured in the file is "<<cnt<<endl;
            }
            index.close();
        }
        system("pause");
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      你在cout之后输了一个'}'

      就这样

      } index.close();

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-24
        • 1970-01-01
        • 1970-01-01
        • 2013-05-02
        • 2016-10-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多