binary data problem

Hey all. I have a problem in reading and writing binary data to a file. Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
#include<fstream>
#include<string>
using namespace 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...
Topic archived. No new replies allowed.