65,184
社区成员




#include<fstream.h>
#include<iostream>
#include<string>
using std::string;
#define keylenth 10
//记录关键字类型
typedef char KeyType[keylenth];
struct ElemType{
KeyType key; //关键字域
string content; //内容域
long length; //整个记录长度
int num; //记录号
};
void main()
{
ElemType e;
cout<<"input key: ";
cin>>e.key ;
e.content="麻烦啊";
e.num =123;
//18=sizeof(e.num)+sizeof(e.key)+sizeof(e.length );
e.length=e.content.size()+1+18;
fstream ofs("1.dat",ios::in|ios::out|ios::binary);
if(!ofs){cout<<"不能打开"<<endl;exit(1);}
ofs.seekp(0,ios::end); //指针移到文件尾
long t=ofs.tellp(); //写入e前的指针位置
ofs.write((char*)&e,e.length);//写入e, 这样子对不对?????
ElemType E;
ofs.seekp(t,ios::beg);//指针定位到写入e前的地址
ofs.read((char*)&E,e.length);
cout<<"从文件中读到的 "<<E.num<<" "<<E.length<<" "<<E.key<<endl;
std::cout<<(E.content);
cout<<endl;
}