Discussion:
Problem saving CString data
(too old to reply)
Me
2013-02-10 22:04:50 UTC
Permalink
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
2013-02-11 05:37:40 UTC
Permalink
Post by Me
I am using Visual Studio 2012 VC++
CFile CString save problem
*************************************************************************************
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?
PressType TeleColor2
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?
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
2013-02-11 00:26:47 UTC
Permalink
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
2013-02-11 13:07:32 UTC
Permalink
Post by Me
PressType TeleColor2
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/
How do I save as 0NE byte characters?
ScottMcP [MVP]
2013-02-11 17:49:31 UTC
Permalink
Post by Me
Post by Me
PressType TeleColor2
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/
How do I save as 0NE byte characters?
For one byte characters use CStringA instead of CString.
Me
2013-02-12 13:31:56 UTC
Permalink
Post by ScottMcP [MVP]
Post by Me
Post by Me
PressType TeleColor2
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/
How do I save as 0NE byte characters?
For one byte characters use CStringA instead of CString.
Thank you Scott, problem solved.

Loading...