Click to See Complete Forum and Search --> : Trouble with CString serialization after formatting?


Red Dorakeen
May 25th, 1999, 01:44 PM
Hi everyone, I'm having a problem with a program I am writing. I'm fairly new to Visual C++, more familiar with the Borland compiler.

Anyway, I'm getting values from the user via a dialogue box (both CStrings and integers) and want to store them in a file for later use/editing. I've got the basics of serialization down, and think I may just be missing something in my code.

Basically, I get these values from the dialogue box, and either use CString's Format function or the sprintf function (depending on whether I want a char* or CString for an output. If I use EITHER of these before storing my string (using CArchive's serializatiion or CFile's Write() function) (e.g. if I have CArchive ar, I stored in the file by

ar << CString1;

my program gives me an invalid page fault. I've tried tracing the problem, which ends up in the .dll files, (and therefore out of my grasp). This is the last command in the function (which may be incorrect, so I'm trying to find out what IS correct) (i.e. in the mainfrm.cpp, it looks something like:


if(dbox.DoModal() == IDOK)

{etc....
formatting takes place to give final version of CString1;
ar << CString1;
}


If I am not formatting the CString (either into a CString or a char*) the archiving works correctly, and no problem results. But as I really need to get this done in some way, I'm looking for some suggestions. Any help will be greatly appreciated. Thanks a lot!

-Red

Jason Teagle
May 26th, 1999, 02:15 AM
If you are using sprintf() on a CString (and therefore using GetBuffer() ), make sure you call ReleaseBuffer() to set the correct string length before trying to do anything else with the CString. However, the Format() method should not cause any problems...