When i am writing a content into a file as a binary, it's quite stored in a binary format but, my problem is during the reading.After the reading i am receiving the content two times. Actually i don't know whether it writes or reads two times.
Here is the code.
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class store
{
protected:
char pass[50];
public:
fstream file;
store();
~store();
void writing();
void reading();
};
store::store()
{
file.open("binary.dat",ios::in | ios::out | ios::app | ios::binary);
}
store::~store();
{
file.close();
}
int main()
{
store b;
b.writing();
b.reading();
return 0;
}
void store::writing()
{
char buf[] = "hello";
strcpy_s(pass,sizeof(buf),buf);
file.write(reinterpret_cast<char*>(&pass),sizeof(pass));
}
void store::reading()
{
file.seekp(0,ios_base::beg);
while(!file.eof())
{
file.read(reinterpret_cast<char*>(&pass),sizeof(pass));
cout<<pass;
}
}