A number of options are available to the user in this program: Option 1: record entry (terminated by EOF key). Option 2: displays records. Option 3: exit program.
The user may then repeat this process. I wish to avoid overwriting records by seeking to the end of the file, and ensure that the close() and open() calls are correct.
After declaring:
fstream fs("file", ios_base::in | ios_base::out | ios_base::binary);
is it necessary to explicitly call open;
fs.open("file", ios_base::in | ios_base::binary);
if it is necessary: must binary mode be specified? and is it necessary to clear the stream prior to writing successively?
struct record
{
char firstname[MAX], lastname[MAX];
int score;
};
int main(){
record r;
// must the modes be listed in a particular order?
fstream fs("file", ios_base::in |ios_base::out | ios_base::binary);
if(choice == 1) // option 1 for writing
{
fs.clear(); // is it necessary to clear?
// is it necessary to explicitly call open [and close] within 'if'
fs.open("file", ios_base::in | ios_base::binary);
fs.seekp(0, ios_base::end); //attempt to avoid overwriting previous records
fs.write( (char *)&r, sizeof(r) );
fs.close(); // is it best to close the file here...
}
else if(choice == 2){ /*Display records*/ }
else if(choice == 3){ exit(1); }
/* would it be incorrect to fs.close() here and not call fs.open() */
return 0;
}
Aucun commentaire:
Enregistrer un commentaire