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 to a file a structure wich contains Cstring ?

how to Read/Write to a file a structure wich contains Cstring ?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
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.
  • T Offline
    T Offline
    timbk
    wrote on last edited by
    #1

    Hello, I have to Write/read a file with a structure wich contains CString data, if drop out the CString data from the structure it works but if I left the CString data the program crashes when attemps to read. Here the structure

    struct MyStructure
    {
    CString label//For others data types char, BYTE, etc it works but for CString it doesn't
    unsignet char data1
    BYTE data2
    }

    the writing

    struct MyStructure Estructura;
    CFile FileConfig;              
    

    FileConfig.Open("ROMconfig.dat",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite);
    FileConfig.Seek(0,CFile::begin);
    FileConfig.Write(&Estructura,sizeof(Estructura));
    FileConfig.Close();

    the reading

    struct MyStructure Estructura;
    CFile FileConfig ;		  
    

    FileConfig.Open("ROMconfig.dat",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeRead);
    FileConfig.Seek(0,CFile::begin);
    FileConfig.Read(&Estructura,sizeof(Estructura));
    FileConfig.Close();

    What I'm doing wrong, may be is something in relation with CString size, but I'm not sure. And the file has to be a .dat file because is modification to an older application.

    C L 2 Replies Last reply
    0
    • T timbk

      Hello, I have to Write/read a file with a structure wich contains CString data, if drop out the CString data from the structure it works but if I left the CString data the program crashes when attemps to read. Here the structure

      struct MyStructure
      {
      CString label//For others data types char, BYTE, etc it works but for CString it doesn't
      unsignet char data1
      BYTE data2
      }

      the writing

      struct MyStructure Estructura;
      CFile FileConfig;              
      

      FileConfig.Open("ROMconfig.dat",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite);
      FileConfig.Seek(0,CFile::begin);
      FileConfig.Write(&Estructura,sizeof(Estructura));
      FileConfig.Close();

      the reading

      struct MyStructure Estructura;
      CFile FileConfig ;		  
      

      FileConfig.Open("ROMconfig.dat",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeRead);
      FileConfig.Seek(0,CFile::begin);
      FileConfig.Read(&Estructura,sizeof(Estructura));
      FileConfig.Close();

      What I'm doing wrong, may be is something in relation with CString size, but I'm not sure. And the file has to be a .dat file because is modification to an older application.

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      You can't save and read your data this way. Internally, a CString object stores its data in a pointer, so you will save only the address of the pointer and not its content. I suggest you read this article[^] which is about serialization in MFC. You'll get some ideas about how to serialize your data properly.

      Cédric Moonen Software developer
      Charting control [v3.0] OpenGL game tutorial in C++

      T 1 Reply Last reply
      0
      • C Cedric Moonen

        You can't save and read your data this way. Internally, a CString object stores its data in a pointer, so you will save only the address of the pointer and not its content. I suggest you read this article[^] which is about serialization in MFC. You'll get some ideas about how to serialize your data properly.

        Cédric Moonen Software developer
        Charting control [v3.0] OpenGL game tutorial in C++

        T Offline
        T Offline
        timbk
        wrote on last edited by
        #3

        Ok, thanks , i will check it, very usefull. But for the other data types is it ok to save data i as described before? , I mean pherhaps is not the elegant way but it's working (not with CString), as I have to finish this work for tomorrow I was hoping there was a easier way, I only have to store some few words.

        C 1 Reply Last reply
        0
        • T timbk

          Ok, thanks , i will check it, very usefull. But for the other data types is it ok to save data i as described before? , I mean pherhaps is not the elegant way but it's working (not with CString), as I have to finish this work for tomorrow I was hoping there was a easier way, I only have to store some few words.

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          It would be easier to provide a function to save and a function to load the structure (functions in the structure itself). These functions will save and load all members of the structure. For the CString type, you first have to save the lenght of the string then the string itself (when loading, you read the lenght and then read that many characters from the file).

          Cédric Moonen Software developer
          Charting control [v3.0] OpenGL game tutorial in C++

          1 Reply Last reply
          0
          • T timbk

            Hello, I have to Write/read a file with a structure wich contains CString data, if drop out the CString data from the structure it works but if I left the CString data the program crashes when attemps to read. Here the structure

            struct MyStructure
            {
            CString label//For others data types char, BYTE, etc it works but for CString it doesn't
            unsignet char data1
            BYTE data2
            }

            the writing

            struct MyStructure Estructura;
            CFile FileConfig;              
            

            FileConfig.Open("ROMconfig.dat",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite);
            FileConfig.Seek(0,CFile::begin);
            FileConfig.Write(&Estructura,sizeof(Estructura));
            FileConfig.Close();

            the reading

            struct MyStructure Estructura;
            CFile FileConfig ;		  
            

            FileConfig.Open("ROMconfig.dat",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeRead);
            FileConfig.Seek(0,CFile::begin);
            FileConfig.Read(&Estructura,sizeof(Estructura));
            FileConfig.Close();

            What I'm doing wrong, may be is something in relation with CString size, but I'm not sure. And the file has to be a .dat file because is modification to an older application.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            If you are using MFC then you could use the CArchive[^] class.

            Just say 'NO' to evaluated arguments for diadic functions! Ash

            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