what happen to my <string> please?
-
Hi, I need a simple and small class that I can serialize to disk (using fstream in binary mode). In the class I have a Standard C++ library string member, but after pumping objects of this class to disk I lost the string value on reading back. The class is something like this; class Person { public: string sName; (private or not doesn't matter) int nAge; Person(const string&Name, int nAgeOfPerson); string GetName() {return sName;}; int GetAge() {return nAge}; } and the ctor, Person(const string&Name, int nAgeOfPerson) { sName = Name; //tried both, don't work. sName = Name.substr(); //using initializing list also not! nAge = nAgeOfPerson; } For my class, sizeof tell me the object is alway and only 28 bytes, even when the name is 29 characters long. I have no problem getting the age back. I guess when I write out to the file the name characters were somehow lost. I want to use Standard C++ Lib and using string but I don't know enough :-O . If I go back the old way using a char szName[50], for example, then everything is OK. But I really don't want to assign 50 bytes for every attribute in the class (small is beautiful for me here). Can someone tell me it is possible to force a value into the string member sName? I am not sure if I make the point clear. Thanks in advance. Will
-
Hi, I need a simple and small class that I can serialize to disk (using fstream in binary mode). In the class I have a Standard C++ library string member, but after pumping objects of this class to disk I lost the string value on reading back. The class is something like this; class Person { public: string sName; (private or not doesn't matter) int nAge; Person(const string&Name, int nAgeOfPerson); string GetName() {return sName;}; int GetAge() {return nAge}; } and the ctor, Person(const string&Name, int nAgeOfPerson) { sName = Name; //tried both, don't work. sName = Name.substr(); //using initializing list also not! nAge = nAgeOfPerson; } For my class, sizeof tell me the object is alway and only 28 bytes, even when the name is 29 characters long. I have no problem getting the age back. I guess when I write out to the file the name characters were somehow lost. I want to use Standard C++ Lib and using string but I don't know enough :-O . If I go back the old way using a char szName[50], for example, then everything is OK. But I really don't want to assign 50 bytes for every attribute in the class (small is beautiful for me here). Can someone tell me it is possible to force a value into the string member sName? I am not sure if I make the point clear. Thanks in advance. Will
The problem is that string stores the string you allocated on the heap, not within the class itself. The class only has a pointer to the string, thus when you write the binary contents of the class (including the string) to disk it saves the pointer to string as it exists right now, but when you reload that data, that data doesn't exist anymore, and thus the pointer is pointing off into nowhere. What you are doing is called "bitwise" persistance, which means it saves the data bit for bit. What you want is called "deep" persistance. And to do this, you have to develop a mechanism to differentiate different types of data in your persistance file. The easiest way to do this is to store the data as text, using the insertion operators of fstream: (assumes using namespace std) void person::opersist(fstream& out) { out << sName << endl << nAgeOfPerson << endl; } void person::ipersis(fstream& in) { in >> sName >> nAgeOfPerson; }
-
The problem is that string stores the string you allocated on the heap, not within the class itself. The class only has a pointer to the string, thus when you write the binary contents of the class (including the string) to disk it saves the pointer to string as it exists right now, but when you reload that data, that data doesn't exist anymore, and thus the pointer is pointing off into nowhere. What you are doing is called "bitwise" persistance, which means it saves the data bit for bit. What you want is called "deep" persistance. And to do this, you have to develop a mechanism to differentiate different types of data in your persistance file. The easiest way to do this is to store the data as text, using the insertion operators of fstream: (assumes using namespace std) void person::opersist(fstream& out) { out << sName << endl << nAgeOfPerson << endl; } void person::ipersis(fstream& in) { in >> sName >> nAgeOfPerson; }
i think u r taking in wrong way. I have to read and write on the contents tab(which appears when u rigth click on any word file and select the properties).i am able to read and write rest all the tabs like (General,Summary,Statstic and custom) but not on contents. I am using IPropertySetStorage. the code i am using is given below please suggest some solution......... ;) Pankaj Mongia