Writing Objects into files in binary mode
-
Writing Object Succeeds with the following code
#include <iostream>
#include <fstream>
using namespace std;class Data
{
public:
string name;
int id;
public:
Data(){}
Data(string n,int id){name=n;this->id=id;}
};int main() {
Data objdata("sujan dhakal",1); fstream file("c:\\\\test.txt",ios::out | ios::binary); if(file.is\_open()) { file.write((char\*)&objdata,sizeof(Data)); cout<<"Success"; file.close(); } else { cout<<"Error"; } return 0;
}
but why reading not working #include <iostream> #include <fstream> using namespace std; class Data { public: string name; int id; public: Data(){} Data(string n,int id){name=n;this->id=id;} }; int main() { Data objdata; fstream file("c:\\test.txt",ios::in | ios::binary); if(file.is_open()) { file.read((char*)&objdata,sizeof(Data)); cout<<objdata.name<<objdata.id<<endl; cout<<"Success"; file.close(); } else { cout<<"Error"; } return 0; } help me
-
Writing Object Succeeds with the following code
#include <iostream>
#include <fstream>
using namespace std;class Data
{
public:
string name;
int id;
public:
Data(){}
Data(string n,int id){name=n;this->id=id;}
};int main() {
Data objdata("sujan dhakal",1); fstream file("c:\\\\test.txt",ios::out | ios::binary); if(file.is\_open()) { file.write((char\*)&objdata,sizeof(Data)); cout<<"Success"; file.close(); } else { cout<<"Error"; } return 0;
}
but why reading not working #include <iostream> #include <fstream> using namespace std; class Data { public: string name; int id; public: Data(){} Data(string n,int id){name=n;this->id=id;} }; int main() { Data objdata; fstream file("c:\\test.txt",ios::in | ios::binary); if(file.is_open()) { file.read((char*)&objdata,sizeof(Data)); cout<<objdata.name<<objdata.id<<endl; cout<<"Success"; file.close(); } else { cout<<"Error"; } return 0; } help me
I suggest you read this series of articles[^]. They are about serrialization, this is what you need to do in order to save/load objects.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
I suggest you read this series of articles[^]. They are about serrialization, this is what you need to do in order to save/load objects.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++i'm writing a simple program using Ansi C++. I'm not using Microsoft C++. that doesnot applies to me. what's your suggestion?
-
Writing Object Succeeds with the following code
#include <iostream>
#include <fstream>
using namespace std;class Data
{
public:
string name;
int id;
public:
Data(){}
Data(string n,int id){name=n;this->id=id;}
};int main() {
Data objdata("sujan dhakal",1); fstream file("c:\\\\test.txt",ios::out | ios::binary); if(file.is\_open()) { file.write((char\*)&objdata,sizeof(Data)); cout<<"Success"; file.close(); } else { cout<<"Error"; } return 0;
}
but why reading not working #include <iostream> #include <fstream> using namespace std; class Data { public: string name; int id; public: Data(){} Data(string n,int id){name=n;this->id=id;} }; int main() { Data objdata; fstream file("c:\\test.txt",ios::in | ios::binary); if(file.is_open()) { file.read((char*)&objdata,sizeof(Data)); cout<<objdata.name<<objdata.id<<endl; cout<<"Success"; file.close(); } else { cout<<"Error"; } return 0; } help me
You cannot do that. You're trying to serialize an object (see "Serialization" topic at Wikipedia [^]) in a wrong way. Since
std::string
is not a scalar type, the object instance (objdata
) holds just a reference to it (and you're saving just such a reference, i.e. storing an address to disk, clearly it makes no sense). So you cannot save the class as a whole usingfile.write((char*)&objdata,sizeof(Data));
instead you have to store each of its members in order to restore appropriately them, for instance:
file.write((char*)&(objdata.id),sizeof(objdata.id));
string::size_type len = objdata.name.length();
file.write((char*) &len, sizeof(len)); // this is useful for reading back the string
file.write((char*)(objdata.name.c_str(), len); //note: not storing null terminatorCan you spot the code for reading back the class? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You cannot do that. You're trying to serialize an object (see "Serialization" topic at Wikipedia [^]) in a wrong way. Since
std::string
is not a scalar type, the object instance (objdata
) holds just a reference to it (and you're saving just such a reference, i.e. storing an address to disk, clearly it makes no sense). So you cannot save the class as a whole usingfile.write((char*)&objdata,sizeof(Data));
instead you have to store each of its members in order to restore appropriately them, for instance:
file.write((char*)&(objdata.id),sizeof(objdata.id));
string::size_type len = objdata.name.length();
file.write((char*) &len, sizeof(len)); // this is useful for reading back the string
file.write((char*)(objdata.name.c_str(), len); //note: not storing null terminatorCan you spot the code for reading back the class? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]so it was only the issue of std::string class, thanks i succeed with the following code.
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;class Data
{
public:
char name[20];
int id;
public:
Data(){}
Data(char *n,int id){strcpy(name,n);this->id=id;}
};int main() {
Data objdata; //fstream file("c:\\\\test.txt",ios::out | ios::binary); fstream file("c:\\\\test.txt",ios::in | ios::binary); if(file.is\_open()) { //file.write((char\*)&objdata,sizeof(Data)); file.read((char\*)&objdata,sizeof(Data)); cout<<objdata.name<<objdata.id; cout<<"Success"; file.close(); } else { cout<<"Error"; } return 0;
}