Can anyone explain me Read/Write structure objects to file

I am BSSE student of First semestor my teacher write like that code but it does not make any sense to me, means i didnt understand this!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void writeData()
{
	wfile.open("students.txt", ios::app); // open file for writing
	for (int i = 0; i < maxSize; i++)
	{
		s[i].getData();
		wfile.write((char *)&s[i], sizeof(s[i]));	//(char*)&s - type casting &s into a char pointer.
													//(char *)&s[i] Pointer to an array of at least n characters.
	}
	wfile.close(); // close the file
}
void readData()
{
	no_of_records = 0;
	rfile.open("students.txt");// , ios::in); // open file for reading
	for (int i = 0; i < maxSize; i++)
	{
		//(char *)&s[i] Pointer to an array where the extracted
		//characters are stored.
		rfile.read((char *)&s[i], sizeof(s[i])); // read an object from a file
		no_of_records++;
	}
	rfile.close(); // close the file
}


when i run the code it save record in like that format

�LUsman C�LHaris��D{Alex@A{Alex�BA�@

how could it be works! when i get data later, when i run program later! how it display data.

here are the full 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include<iostream>
#include<fstream>
using namespace std;
// define a structure to store student data
struct student {
	int sapid;
	char name[30];
	float marks;
	void getData();		// get student data from user
	void displayData(); // display data
};
void student::getData() {
	cout << "\nEnter SAPId. : ";
	cin >> sapid;
	cin.ignore(); // ignore the newline char inserted when you press enter
	cout << "Enter Name : ";
	cin.getline(name, 30);
	cout << "Enter Marks : ";
	cin >> marks;
}

void student::displayData() {
	cout << "SAP Id. : " << sapid << endl;
	cout << "Name : " << name << endl;
	cout << "Marks : " << marks << endl;
}
void menu();
void writeData();
void readData();
void displayData();
void deleteData();

int maxSize = 3;
int no_of_records = 0;
student s[3]; // array of 3 students
ofstream wfile;
ifstream rfile;
//to count to number of students after deletion

int main() {
	int choice;
	while (true)
	{
		menu();
		cin >> choice;
		switch (choice)
		{
		case 1:
			writeData();
			break;
		case 2:
			readData();
			break;
		case 3:
			displayData();
			break;
		case 4:
			deleteData();
			break;
		case 5:
			exit(0);
		default:
			break;
		}//end switch()
	}//end while()
	return 0;
}//end main()
void menu()
{
	cout << "Press 1 to write to the file" << endl;
	cout << "Press 2 to read from file" << endl;
	cout << "Press 3 to dispaly student data" << endl;
	cout << "Press 4 to delete a student data" << endl;
	cout << "Press 5 to exit" << endl;
}

void writeData()
{
	wfile.open("students.txt", ios::app); // open file for writing
	for (int i = 0; i < maxSize; i++)
	{
		s[i].getData();
		wfile.write((char *)&s[i], sizeof(s[i]));	//(char*)&s - type casting &s into a char pointer.
													//(char *)&s[i] Pointer to an array of at least n characters.
	}
	wfile.close(); // close the file
}
void readData()
{
	no_of_records = 0;
	rfile.open("students.txt");// , ios::in); // open file for reading
	for (int i = 0; i < maxSize; i++)
	{
		//(char *)&s[i] Pointer to an array where the extracted
		//characters are stored.
		rfile.read((char *)&s[i], sizeof(s[i])); // read an object from a file
		no_of_records++;
	}
	rfile.close(); // close the file
}

void displayData()
{
	for (int i = 0; i < no_of_records; i++) {
		s[i].displayData();
	}
}
void deleteData()
{
	int sapid, count = 0;
	wfile.open("students.txt");
	cout << "enter student SAPId to delete it:";
	cin >> sapid;

	for (int i = 0; i < maxSize; i++)
	{
		if (s[i].sapid != sapid)
		{
			wfile.write((char *)&s[i], sizeof(s[i]));
			count++;
		}
	}
	wfile.close();
	maxSize = count;
	readData();
}


Thanks,
Last edited on
The problem is that you open the file in text mode but use read and write of the stream which are normally used for binary files. Your code should work if you open them in binary mode.
http://www.cplusplus.com/reference/fstream/ofstream/ofstream/
Maybe your question is about how strructs and classes are stored at binary mode and how it's possible that they could be read and written with relatively small storage size.

It's because at objects the member variables and the methods lay at different storage places. All objects of the same type are sharing their set of methods. These methods lay within the program storage place, whereas the members reside within the stack (or the heap). As still mentioned, all methods are unique and will be shared by all its associated objects. The connections between the methods and object members lay in the response of the compiler. Here an example:

Look at the example below:
1
2
3
4
5
6
7
8
9
struct S { int dat; int get(); };
int S::get() { return dat; }

S s[3];

int main() 
{
    return s[0].get() + s[1].get() + s[2].get();
}


The compiler generates from this the following assembly 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
S::get():
        pushq   %rbp
        movq    %rsp, %rbp
        movq    %rdi, -8(%rbp)
        movq    -8(%rbp), %rax
        movl    (%rax), %eax
        popq    %rbp
        ret
s:
        .zero   12
main:
        pushq   %rbp
        movq    %rsp, %rbp
        pushq   %rbx
        movl    $s, %edi
        call    S::get()
        movl    %eax, %ebx
        movl    $s+4, %edi
        call    S::get()
        addl    %eax, %ebx
        movl    $s+8, %edi
        call    S::get()
        addl    %ebx, %eax
        movq    -8(%rbp), %rbx
        leave
        ret

You can see, that the method is seperately stored from its member, and that there will be no connectiion to the method stored ( like a pointer ).

okay, thanks to all.
Topic archived. No new replies allowed.