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. How to read/write an object of MyClass from/to a file.

How to read/write an object of MyClass from/to a file.

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++helptutorial
5 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.
  • S Offline
    S Offline
    six_billion
    wrote on last edited by
    #1

    I want using VC++ to read/write an object of MyClass from/to a file. I've write CFile myFile; ... myFile.Write((char*)&myObject, sizeof(myObject)); So in my file there is a stranger string :( How can I do properly? I'm a beginning of programing. Please help me! Best regard!

    K RaviBeeR S 3 Replies Last reply
    0
    • S six_billion

      I want using VC++ to read/write an object of MyClass from/to a file. I've write CFile myFile; ... myFile.Write((char*)&myObject, sizeof(myObject)); So in my file there is a stranger string :( How can I do properly? I'm a beginning of programing. Please help me! Best regard!

      K Offline
      K Offline
      KalliMan
      wrote on last edited by
      #2

      Hi, You cann't write directly class into File. You must use Serialization, and serialize each memeber from your class. See the sample below and look into MSDN for more Info. class CPerson : public CObject { public: DECLARE_SERIAL( CPerson ) // empty constructor is necessary CPerson(){}; CString m_name; WORD m_number; void Serialize( CArchive& archive ); // rest of class declaration }; void CPerson::Serialize( CArchive& archive ) { // call base class function first // base class is CObject in this case CObject::Serialize( archive ); // now do the stuff for our specific class if( archive.IsStoring() ) archive << m_name << m_number; else archive >> m_name >> m_number; } a

      S 1 Reply Last reply
      0
      • S six_billion

        I want using VC++ to read/write an object of MyClass from/to a file. I've write CFile myFile; ... myFile.Write((char*)&myObject, sizeof(myObject)); So in my file there is a stranger string :( How can I do properly? I'm a beginning of programing. Please help me! Best regard!

        RaviBeeR Offline
        RaviBeeR Offline
        RaviBee
        wrote on last edited by
        #3

        See this[^] tutorial. /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

        1 Reply Last reply
        0
        • S six_billion

          I want using VC++ to read/write an object of MyClass from/to a file. I've write CFile myFile; ... myFile.Write((char*)&myObject, sizeof(myObject)); So in my file there is a stranger string :( How can I do properly? I'm a beginning of programing. Please help me! Best regard!

          S Offline
          S Offline
          six_billion
          wrote on last edited by
          #4

          thanks all! :)

          1 Reply Last reply
          0
          • K KalliMan

            Hi, You cann't write directly class into File. You must use Serialization, and serialize each memeber from your class. See the sample below and look into MSDN for more Info. class CPerson : public CObject { public: DECLARE_SERIAL( CPerson ) // empty constructor is necessary CPerson(){}; CString m_name; WORD m_number; void Serialize( CArchive& archive ); // rest of class declaration }; void CPerson::Serialize( CArchive& archive ) { // call base class function first // base class is CObject in this case CObject::Serialize( archive ); // now do the stuff for our specific class if( archive.IsStoring() ) archive << m_name << m_number; else archive >> m_name >> m_number; } a

            S Offline
            S Offline
            six_billion
            wrote on last edited by
            #5

            I've got the error: error LNK2001: unresolved external symbol "public: virtual struct CRuntimeClass * __thiscall CStudent::GetRuntimeClass(void)const " (?GetRuntimeClass@CStudent@@UBEPAUCRuntimeClass@@XZ) Please help!

            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