I use this

#include <fstream>
#include <iostream>
using namespace std;

class Student
{
public:
	char   FullName[40];
	char   CompleteAddress[120];
	char   Gender;
	double Age;
	bool   LivesInASingleParentHome;
};

int main()
{
	Student one;

	strcpy(one.FullName, "Ernestine Waller");
	strcpy(one.CompleteAddress, "824 Larson Drv, Silver Spring, MD 20910");
	one.Gender = 'F';
	one.Age = 16.50;
	one.LivesInASingleParentHome = true;
	
	ofstream ofs("fifthgrade.ros", ios::binary);

	ofs.write((char *)&one, sizeof(one));

	return 0;
}

and output in the file is

"Ernestine Waller ( ภ( ุ" ”์รu ( 824 Larson Drv, Silver Spring, MD 20910 ๓’ ุ" “๑รu ์" fฃรu๐แฬu," โ์รu ฮ1ฤuง1ฤu`ด0] F๘@ " €0@$ลuไzึ("

it seem string data can be see from the file but I want it can't read. how can I solve it?

Recommended Answers

All 2 Replies

Well i personally didnt understand the char * cast that you put in the write.

I would recommend that you should write in a << operator for your class . Such that it sends in the data as it is required.

int Student::operator << (&ofstream out)
{
out<<FullName<<CompleteAddress<<Gender<<Age<<LivesInASingleParentHome<<endl;
} ;

I MAY NOT BE CONVINCINGLY CORRECT
Please forgive me if my code is wrong or if there is another simple method/

#include <fstream>
#include <iostream>
using namespace std;

class Student
{
public:
	char   FullName[40];
	char   CompleteAddress[120];
	char   Gender;
	double Age;
	bool   LivesInASingleParentHome;
};

int main()
{
	Student one;

	strcpy(one.FullName, "Ernestine Waller");
	strcpy(one.CompleteAddress, "824 Larson Drv, Silver Spring, MD 20910");
	one.Gender = 'F';
	one.Age = 16.50;
	one.LivesInASingleParentHome = true;
	
	ofstream ofs("fifthgrade.ros", ios::binary);

	ofs.write((char *)&one, sizeof(one));

	return 0;
}#include <fstream>
#include <iostream>
using namespace std;

class Student
{
public:
	char   FullName[40];
	char   CompleteAddress[120];
	char   Gender;
	double Age;
	bool   LivesInASingleParentHome;
};

int main()
{
	Student one;

	strcpy(one.FullName, "Ernestine Waller");
	strcpy(one.CompleteAddress, "824 Larson Drv, Silver Spring, MD 20910");
	one.Gender = 'F';
	one.Age = 16.50;
	one.LivesInASingleParentHome = true;
	
	ofstream ofs("fifthgrade.ros", ios::binary);

	ofs.write((char *)&one, sizeof(one));

	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.