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
�L Usman C�L Haris ��D{ Alex @A{ Alex �B A �@
how could it be works! when i get data later, when i run program later! how it display data.
#include<iostream>
#include<fstream>
usingnamespace 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();
}
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: