cfile ,读一个记事本文件,把读出的内容显示在EDIT控件中,为什么后面有几个乱码,该怎么处理
cfile ,读一个记事本文件,把读出的内容显示在EDIT控件中,为什么后面有几个乱码
CFile f;
CFileException e;
char* pFileName = "test.txt ";
if( !f.Open( pFileName, CFile::modeReadWrite, &e ) )
{
#ifdef _DEBUG
afxDump < < "File could not be opened " < < e.m_cause < < "\n ";
#endif
}
else
{
char* pbuf = new char[10];
//char pbuf[10];
UINT nBytesRead = f.Read(pbuf, 7);
m_edit1 = pbuf;
UpdateData(FALSE);
}
编辑框里的乱码为hello!屯屯葺
谢谢!
------解决方案--------------------
靠,你new了10个字节的缓冲区,没有初始化,只读了7个字节,所以剩下的3个字符是未初始化的乱码咯.
你按照我以下的代码试试?
char* pbuf = new char[10];
memset(pbuf,0,10);
UINT nBytesRead = f.Read(pbuf, 7);
m_edit1 = pbuf;
UpdateData(FALSE);
------解决方案--------------------
你new了的内存长度10,但你只读了7阿
你改成char* pbuf = new char[8];
试试
------解决方案--------------------
操作文本文件用CStdioFile方便
------解决方案--------------------
没必要用new,用了你也没有delete,内存泄漏的!
直接定义
char buf[10];
然后初始化再使用
memset( buf, '\0 ', sizeof(buf) );
CFile f;
CFileException e;
char* pFileName = "test.txt ";
if( !f.Open( pFileName, CFile::modeReadWrite, &e ) )
{
#ifdef _DEBUG
afxDump < < "File could not be opened " < < e.m_cause < < "\n ";
#endif
}
else
{
char* pbuf = new char[10];
//char pbuf[10];
UINT nBytesRead = f.Read(pbuf, 7);
m_edit1 = pbuf;
UpdateData(FALSE);
}
编辑框里的乱码为hello!屯屯葺
谢谢!
------解决方案--------------------
靠,你new了10个字节的缓冲区,没有初始化,只读了7个字节,所以剩下的3个字符是未初始化的乱码咯.
你按照我以下的代码试试?
char* pbuf = new char[10];
memset(pbuf,0,10);
UINT nBytesRead = f.Read(pbuf, 7);
m_edit1 = pbuf;
UpdateData(FALSE);
------解决方案--------------------
你new了的内存长度10,但你只读了7阿
你改成char* pbuf = new char[8];
试试
------解决方案--------------------
操作文本文件用CStdioFile方便
------解决方案--------------------
没必要用new,用了你也没有delete,内存泄漏的!
直接定义
char buf[10];
然后初始化再使用
memset( buf, '\0 ', sizeof(buf) );