Code
#include <iostream>
#include <fstream>
struct emp
{
char name[20];
int age;
};
int main()
{
emp e1={"Abhishek", 22},e2;
std::ofstream fout;
fout.open("vicky.dat", std::ios::out | std::ios::binary);
fout.write(reinterpret_cast<const char*>(&e1),24);
fout.close();
std::ifstream fin;
fin.open("vicky.dat", std::ios::in | std::ios::binary);
fin.read(reinterpret_cast<char*>(&e2),24);
fin.close();
std::cout<<e2.name<<" "<<e2.age<<"\n";
return 0;
}
Why it is necessary to do reinterpret_cast
with 1st argument of write
and read
function ?
why we casting address of type emp
particularly to const char*
and char*
in write
and read
function respectively ?