3全局声明文件I / O流(在所有函数之外)
ifstream fin;
ofstream fout;
fstream file;
这些是出现问题的2个功能。由于某种原因,无法打开文件“quizquestions.dat”。由于声明退出(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();
}
答案 0 :(得分:0)
尝试使用fout.open(ios :: app | ios:binary | ios :: out);