Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. what happen to my <string> please?

what happen to my <string> please?

Scheduled Pinned Locked Moved C / C++ / MFC
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    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

    E 1 Reply Last reply
    0
    • L Lost User

      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

      E Offline
      E Offline
      Erik Funkenbusch
      wrote on last edited by
      #2

      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; }

      P 1 Reply Last reply
      0
      • E Erik Funkenbusch

        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; }

        P Offline
        P Offline
        Pankaj Mongia
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups