| Arashdn (8) | |||
|
Hi, I am working on a MFC application witch needs to store data on a file Here in my App
When i run the app and click on button I got this message box: Name:N2 ID:22222 But I expect it to be Name:Name ID:11111 What is wrong with this app? edit: I used char * and ofstream and it's working well (this part solved , just second question is remained) And , How can I write data to file using a function inside of my class? Thanks | |||
|
Last edited on
|
|||
| r4zzz4k (8) | |||
|
Hello. I think, your main problem is trying to serialize/deserialize your object using wide stream. wifstream::read uses its second parameter as number of characters to read. But you should take into account that every wchar_t can be 1, 2, or 4 bytes depending on operating system and compiler. So using there simply sizeof(myCl) can drive you into trouble. In fact, the best decision will be to use char streams to write/read objects to/from file.
So reading and writing runs byte-by-byte. By the way, as I know, MSVC and GCC compilers store pointer to virtual methods table on negative offsets from object pointer, so serializing of objects vith virtual methods like that can offer you a bit of headache because of breaking of objects. | |||
|
Last edited on
|
|||
| Arashdn (8) | |||||
|
Thanks for reply. That was problem for reading Data I have created a function like this inside of my Client Class:
And I use function Like this:
But I got this error when i run the function (in debuging ) Unhandled exception at 0x5adfab2a (mfc100ud.dll) in MyProject.exe: 0xC0000005: Access violation writing location 0x039708fc. What is the problem? | |||||
|
Last edited on
|
|||||
| r4zzz4k (8) | |
Wariable errno staggers me, I don't see, where it comes from. I am not familiar with MFC, though, maybe it's born there. But I can tell you, that ifstream/ofstream won't change any global variables to report you an error message, so I assume you sould write error text by yourself in place of errno.To the problem. You see, your update method works onlu with its parameter and doesn't need this pointer. So you should definitely make this method static. My point is when you call non-member method on your object and try to rewrite this object, you get an access violation. So try to make it static and tell me, if it goes right way.
| |
|
|
|
| modoran (1245) | |
|
I'd use MFC own CArchive class instead of that: http://msdn.microsoft.com/en-us/library/caz3zy5s(v=vs.100).aspx If you already use a complex framework like MFC, why you don't use the features you have ? Mixing MFC classes with C++ STL classes is bad style. MFC has classes like CFile which you could use instead of std::ifstream. http://msdn.microsoft.com/en-us/library/60fh2b6f(v=vs.80).aspx | |
|
|
|
| Arashdn (8) | |||
|
Thanks for replies \
Unfortunately , making method static didn't change any thing ... Is there any way not to have any arguments and make changes in current object?
These data must be accessible on another native application which I am developing ... | |||
|
Last edited on
|
|||
| r4zzz4k (8) | ||||
Well, you can don't pass any parameter to method and use inside this, because it's a pointer to the current object, but I believe, rewriting data by-byte isn't good style anyway, especially in member method through this ptr.I've managed to get rid of exception by defining of second client in a different way: Client* myCl2 = (Client*)new char[sizeof(Client)];Here, as you can see, myCl2 points to a block of memory with needed size, and not to really created object. Of course, you should fill it before making any call to it's members, that's why non-static member isn't really an option. If I were you, I would make a static method like that:
And if you will do some more complex serialization, you should read this: http://www.parashift.com/c++-faq-lite/serialization.html Hope this will help. | ||||
|
Last edited on
|
||||
| Arashdn (8) | |||
|
Thanks a lot, How can I use this function? I tried
But It seems not to be correct. | |||
|
Last edited on
|
|||
| r4zzz4k (8) | |
Function returns pointer, soClient* myClient = Client::update();And dont forget to do delete myClient;when you've finished to work with it! PS. If you feel you're not familiar with pointers or some other part of language, I advise you to grab some good book on C++ and read it carefully. And walk through C++-faq linked by me above, there are many interesting things there for any programmer. | |
|
Last edited on
|
|
| Arashdn (8) | |||
|
Thanks I tried this
But I got Access violation error Is it wrong? | |||
|
Last edited on
|
|||
| Arashdn (8) | |||
I also tried
But I got Access violation error .... | |||
|
|
|||
| r4zzz4k (8) | |
Well, post please your current version of update. I can't realize, why is there a parameter and what are you doing inside that method.
| |
|
|
|
| Arashdn (8) | |||||
my update is same as this
and I use it like:
And I got Access violation error | |||||
|
|
|||||
| r4zzz4k (8) | |
|
I really advice you to read some teach-book on C++ to understand all basic concepts before moving somewhere. Do you understand difference between static method and non-static one? If so, do you see any using of this pointer in method body? In oter words, does call to update uses myClient in any way? If not, this method should be static.To the error. I can't reproduce it. Can you post error message here and tell, in which line does it occure? | |
|
|
|
| Arashdn (8) | |||
|
Well , Let's start a new application A simple one I have a dialog and just one button in it: and here is all of my code:
error is :Unhandled exception at 0x5ed2b1d0 (msvcr100d.dll) in test_mfc.exe: 0xC0000005: Access violation reading location 0xcc006161. at line 62. What Can the problem be? | |||
|
Last edited on
|
|||
| modoran (1245) | |
Use c_str() method, CString ::Format expects a regular C style string as argument for "%s", not a std::string as you do.mess.Format(L"Name:%s\nID:%s",myCl2.name.c_str(), myCl2.id.c_str() );
| |
|
|
|