文件怎么弄?希望大家指出我的错误。先谢谢大家啦
#include "iostream.h"#include "fstream.h"
#include "string.h"
#define MAX 100
struct WORD
{
char english[10];
char chinese[20];
};
class dic
{
private:
WORD word[MAX];
public:
int n;
dic();
void input();
void check();
void show();
};
dic::dic()
{
n=1;
}
void dic::input()
{
cout<<"english:";
cin>>word[n].english;
cout<<"chinese:";
cin>>word[n].chinese;
ofstream file("dictionary.dat",ios::out|ios::app);
file.write((char*)&word[n],sizeof(WORD));
n++;
}
void dic::check()
{
int j=1;
bool isfind=false;
char checkname[10];
cout<<"checkname;";
cin>>checkname;
ifstream checkfile("dictionary.dat");
if(!checkfile)cout<<"open error!";
while(checkfile.read((char*)&word[j],sizeof(WORD)))
{
if(strcmp(checkname,word[j].english)==0)
{
cout<<"chinese:"<<word[j].chinese;
isfind=true;
}
else j++;
}
if(!isfind)cout<<"没有找到!"<<endl;
}
void dic::show()
{
int j=1;
ifstream infile("dictionary.dat");
if(!infile)cout<<"open error!"<<endl;
while(infile.read((char*)&word[j],sizeof(WORD)))
{
cout<<word[j].english<<word[j].chinese<<endl;
j++;
}
}
void main()
{
dic dictionary;
int N,m=1;
cout<<"...............字典..........................."<<endl;
cout<<"1..............输入单词......................."<<endl;
cout<<"2..............查找单词......................."<<endl;
cout<<"3..............显示字典库....................."<<endl;
cout<<"4..............退出..........................."<<endl;
while(m)
{
cin>>N;
switch(N)
{
case 1: dictionary.input();break;
case 2: dictionary.check();break;
case 4: dictionary.show();break;
case 5: m=0;break;
}
}
}