-
binary file
can someone pls tell me what is wrong with my program
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream rfile;
rfile.open("ITEMS.DAT", ios::in|ios::out);
if(!rfile)
{
cout<<"error\n.";
exit(1);
}
int c=0;
cout<<"Enter c\n";
cin>>c;
cin.get();
rfile.seekp(0, ios::end);
rfile.write( (char*) (&c), sizeof(int) )<<flush;
rfile.seekg(0, ios::beg);
while (!rfile.eof())
{
rfile.read( (char*) (&c), sizeof(int));
cout<<"c is:"<<c<<endl;
}
rfile.clear();
rfile.close();
return 0;
}
// the output is this
//enter c
// c is:2
// c is:2
question: why are there two outputs of 2 even though the file is clean and i only enter one 2?
-
thank u salem
thank u salem you have been a great help:)