#include<iostream>
#include<fstream>
#include<string>
usingnamespace std;
class Player
{
public:
float x;
float y;
float z;
short playerStatus;
std::string playerName;
};
void Read()
{
int size=0;
ifstream r("C:\\kav\\PacketForm.txt",std::ios::binary);
Player p1;
size=sizeof p1;
r.read((char*)&p1,size);
r.close();
cout<<p1.playerStatus<<endl;
cout<<p1.x<<endl;
cout<<p1.y<<endl;
cout<<p1.z<<endl;
system("pause");
}
template<class Packet>
void Write(Packet p)
{
int size=0;
size=sizeof(p);
ofstream w("C:\\kav\\PacketForm.txt",std::ios::binary);
w.write((char*)&p,size);
w.close();
system("pause");
}
void main()
{
Player p;
p.x=10;
p.y=0;
p.z=-5;
p.playerStatus=1;
p.playerName="Hello... this is a test service for writing and reading data as a binary form.";
Write(p);
Read();
return;
}
The problem is in the string since reserving a string of non variable size char playerName[128]; it works fine. but i need a variable string for my application. Please advise and tnx in advance...