0

3 文件 I/O 流是全局声明的(在所有函数之外)

ifstream fin;
ofstream fout;
fstream file;

这是发生问题的两个功能。由于某种原因,无法打开文件“quizquestions.dat”。由于该语句 exit(1); 正在执行,程序正在终止。问题编号未正确分配。对象“通用”是我的类问题的对象,它存储问题。代码被编译并且没有错误。

void questionnumber()
{
    int file_size=0;
    fin.open("quizquestions.dat",ios::in|ios::binary);
    fin.seekg(0,ios::end);
    file_size = file.tellg();
    if(file_size==0)
        universal.assignquestion(1);
    fin.close();
    int next_question = (file_size/sizeof(question)) + 1;
    universal.assignquestion(next_question);
}
void addquestion()
{
    ch='y';
    do
    {
        cout<<"Adding question now\n";
        cout<<"Please\n";
        universal.createquestion();
        questionnumber();
        fout.open("quizquestions.dat",ios::app|ios::binary);
        if(!fout)
            exit(1);
        fout.write((char*)&universal,sizeof(question));
        fout.close();
        cout<<"Your question is added\nYour question details are as follows"<<endl;
        universal.dispquestion();
        cout<<"Do you want to add more questions?(Y/N)\n";
        cin>>ch; cin.ignore();
    }
    while(ch=='y'||ch=='Y');
    if(ch!='y'||ch!='Y')
        dispsetting();
}
4

1 回答 1

0

尝试使用 fout.open(ios::app | ios:binary | ios::out);

于 2012-10-07T16:22:14.667 回答