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