Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem saving CString data

22 views
Skip to first unread message

Me

unread,
Feb 10, 2013, 5:04:50 PM2/10/13
to
I am using Visual Studio 2012 VC++

CFile CString save problem
I have the following:

*************************************************************************************
CFile f;
int flen;
CString Afile;
CString Ptype;
CString Tstr1;

// m_strPressType is a control variable of an edit box
m_strPressType.GetWindowTextW(Ptype); //retrieve contents of editbox

Tstr1.Format(_T("PressType %s"), Ptype); //create save string

// verify length of the save file which shows correct length
flen = Tstr1.GetLength();
Tstr2.Format(_T("flen = %d"), flen);
AfxMessageBox(Tstr2);

//save string to file
CFileException e;
Afile.Format(_T("%soptions.ini"), BasePath); //BasePath is type char
"C:\MyData\"
if(!f.Open(Afile, CFile::modeCreate || CFile::modeWrite, &e))
afxDump << "File could not be opened " << e.m_cause << "\n"; //no error
displayed
f.Write(Tstr1, flen); //write the string to file
f.Close();

*************************************************************************************

The filename is created successfully but the contents are incorrect.
flen is displayed as 20 bytes which would be correct but the file saved is
218 bytes?

The contents SHOULD BE:
PressType TeleColor2

But the actual contents is:
P r e s s T y p e
garbage follows here after CRLF for the rest of the 218 bytes.....

What am I doing wrong?

Geoff

unread,
Feb 11, 2013, 12:37:40 AM2/11/13
to
The code looks correct but I notice you don't f.Flush the file before you close
it. It might make a difference.

I would also set a breakpoint at f.Write and inspect Tstr1 and flen to be sure
they are the values you expect.

David Webber

unread,
Feb 10, 2013, 7:26:47 PM2/10/13
to
> The contents SHOULD BE:
PressType TeleColor2

But the actual contents is:
P r e s s T y p e
garbage follows here after CRLF for the rest of the 218 bytes.....

What am I doing wrong?<

Well one thing: it looks like you're saving 2-byte characters, and looking
at the file contents as if they're supposed to be single byte characters -
hence the spaces.

Dave

-- David Webber
Mozart Music Software
http://www.mozart.co.uk/

Me

unread,
Feb 11, 2013, 8:07:32 AM2/11/13
to
How do I save as 0NE byte characters?

ScottMcP [MVP]

unread,
Feb 11, 2013, 12:49:31 PM2/11/13
to
For one byte characters use CStringA instead of CString.

Me

unread,
Feb 12, 2013, 8:31:56 AM2/12/13
to
Thank you Scott, problem solved.
0 new messages