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. bad pointer error when I read a class object with CString member

bad pointer error when I read a class object with CString member

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelptutorial
4 Posts 4 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.
  • M Offline
    M Offline
    manoharbalu
    wrote on last edited by
    #1

    class test { public: CString str; } test oTest; oTest.str = "abc"; When I try to write the above object 'oTest' to a simple file using fwrite and CFile and then try to read using fread, I am getting bad pointer in oTest.str But when I replace the CString member with char array as "char str[50]" and try to write and read file, its working So I want to know how to write and read a class object with CString member to and from a file

    S D S 3 Replies Last reply
    0
    • M manoharbalu

      class test { public: CString str; } test oTest; oTest.str = "abc"; When I try to write the above object 'oTest' to a simple file using fwrite and CFile and then try to read using fread, I am getting bad pointer in oTest.str But when I replace the CString member with char array as "char str[50]" and try to write and read file, its working So I want to know how to write and read a class object with CString member to and from a file

      S Offline
      S Offline
      Sascha Lefevre
      wrote on last edited by
      #2

      Please don't post the same question in multiple places. You already posted it in Quick Answers: Bad Pointer Error when I read a class object with CString member[^]

      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

      1 Reply Last reply
      0
      • M manoharbalu

        class test { public: CString str; } test oTest; oTest.str = "abc"; When I try to write the above object 'oTest' to a simple file using fwrite and CFile and then try to read using fread, I am getting bad pointer in oTest.str But when I replace the CString member with char array as "char str[50]" and try to write and read file, its working So I want to know how to write and read a class object with CString member to and from a file

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        manoharbalu wrote:

        When I try to write the above object 'oTest' to a simple file using fwrite and CFile and then try to read using fread, I am getting bad pointer in oTest.str

        Seeing the ACTUAL code would be helpful. Just sayin'...

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        1 Reply Last reply
        0
        • M manoharbalu

          class test { public: CString str; } test oTest; oTest.str = "abc"; When I try to write the above object 'oTest' to a simple file using fwrite and CFile and then try to read using fread, I am getting bad pointer in oTest.str But when I replace the CString member with char array as "char str[50]" and try to write and read file, its working So I want to know how to write and read a class object with CString member to and from a file

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

          fwrite only stores the memory contents of the class test - it doesn't care about the class definition except for it's size in memory. CString stores the text in dynamic memory, only handle (pointer) to that memory is directly stored inside the CString object, therefore writing oTest just stores that pointer, but not the contents. If you use char[50] instead, that is a fixed-size array that is stored fully in your class test, and therewore fwrite will write it to file correctly. The correct way to implement streaming for your test class in C++ is by overloading the streaming operators. See http://www.tutorialspoint.com/cplusplus/input_output_operators_overloading.htm[^]

          GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

          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