Вы находитесь на странице: 1из 3

/**************************************************************************

*Exampleofarandomaccessfileusingios::binary
*
*ios::inthedefaultmodeforifstreamobjects.openafileforinput
*nondestructively,withthereadpositionatthefile's
*beginning.Willcreatefileifitdoesn'texist.
*ios::truncOpenafileanddeleteanycontentsitcontains.
*ios::outThedefaultmodeforofstreamobjects.Openafileforoutput
*usingios::trunc.
*ios::appOpenafileforoutput,butnondestructively,withthewrite
*positionatthefile'send.
*ios::ateOpenanexistingfilewiththereadpositionorwriteposition
*attheendofthefile.
*ios::binaryOpenafileinbinarymode.
*
*Copyright2001EnochHwang
*/

#include<iostream>
#include<fstream>
#include<string>
usingnamespacestd;

intmain(){
charInputFileName[]="data.bin";
structdatarecords{
intID;
charName[20];
intAge;
};
datarecordsrecord;
intrecordnumber;
intnumberofrecords=0;
charname[20];

fstreamMyStream;

//testiffileexistsornot
MyStream.open(InputFileName,ios::in|ios::out|ios::binary);
if(MyStream){
//oldfile
MyStream>>numberofrecords;
cout<<"Oldfilewith"<<numberofrecords<<"records."<<endl;
}else{
//newfile
MyStream.close(); //needtocreatethefile
MyStream.open(InputFileName,ios::in|ios::out|ios::binary|
ios::trunc);

numberofrecords=0;
cout<<"Newfile"<<endl;
}

/**************************************************************************
*Writerandomfile
*/
for(;;){
cout<<"EnterrecordnumbertoWRITE(0=end)?";
cin>>recordnumber;
if(recordnumber<=0)break;

//rememberthemaximumnumberofrecordsenteredsofar
if(recordnumber>numberofrecords)
numberofrecords=recordnumber;

//enternewdata
record.ID=recordnumber;
cout<<"Enterthename?";
cin>>name;
strcpy(record.Name,name);
cout<<"Entertheage?";
cin>>record.Age;

//movewritepointertocorrectlocationinfile
//theextrasizeof(int)spaceisforstoringthenumberofrecords
//atthebeginningofthefile
MyStream.seekp((recordnumber1)*sizeof(datarecords)+sizeof(int),
ios::beg);
cout<<"Recordwrittentolocation"<<MyStream.tellp()<<endl;

//writedatatofile
//Notethatthefirstargumenttowritemustbeoftype<constchar*>.
//However,&recordisoftypedatarecords*.
//Thus,weneedtoconvertthetypeusingreinterpret_cast<constchar*>
MyStream.write(reinterpret_cast<constchar*>(&record),
sizeof(datarecords));
}

//writenumberofrecordstothebeginningofthefile
MyStream.seekp(0,ios::beg);
MyStream<<numberofrecords;

cout<<endl<<"*******************************************"<<endl<<endl;

/**************************************************************************
*Readrandomfile
*/
for(;;){
cout<<"EnterrecordnumbertoREAD(0=end)?";
cin>>recordnumber;
if(recordnumber<=0)break;

//makesurethatrecordtoretrieveisnotpassedeof
if(recordnumber>numberofrecords){
cout<<"Passeof"<<endl;
continue;
}

//movereadpointertocorrectlocationinfile
//theextrasizeof(int)spaceisforstoringthenumberofrecordsatthe
beginningofthefile
MyStream.seekg((recordnumber1)*sizeof(datarecords)+sizeof(int),
ios::beg);
cout<<"Record"<<recordnumber<<"startsatlocation"<<
MyStream.tellg()<<endl;

//readdatafromfile
//Notethatthefirstargumenttoreadmustbeoftype<char*>.
//**Becarefulthatthereadtypeisdifferentfromthewritetype.
//However,&recordisoftypedatarecords*.
//Thus,weneedtoconvertthetypeusingreinterpret_cast<char*>
MyStream.read(reinterpret_cast<char*>(&record),sizeof(datarecords));
cout<<"ID:"<<record.ID<<endl;
cout<<"Name:"<<record.Name<<endl;
cout<<"Age:"<<record.Age<<endl;
}

MyStream.close();
if(MyStream.fail())cerr<<"9";

return0;
}

Вам также может понравиться