为什么函数GetPrivateProfileSection会返回乱码?
我创建了一个对话框用于测试GetPrivateProfileSection函数,写了以下几个函数,其中OnWrtfile
和OnReadfile分别是写文件和读文件两个按钮的消息处理函数.具体操作步骤是先按写文件按钮,执
行OnWrtfile函数,再按读文件按钮,执行OnReadfile函数.遇到的问题用注释给出了.
void CLchQQTestDlg::OnWrtfile(){
WritePrivateProfileString("initest","name","lisi",".\\test.ini");
}
void CTestDlg::OnReadfile(){
GetAllData();//调用GetAllData()
}
char* CLchQQTestDlg::GetAllData(){
char buf[MAX_PATH];
GetPrivateProfileSection("initest",(LPSTR)buf,MAX_PATH,(LPCSTR)".\\test.ini");
int len=strlen(buf);
for(int i=0;i<len;i++){
CString str;
str.Format("%c",buf[i]);
AfxMessageBox(str);//没有问题,显示正常
}
return buf;
}
但是如果我把OnReadfile和GetAllData写成下面这样,就出现乱码了.
void CTestDlg::OnReadfile(){
char* buf=GetAllData();//调用GetAllData();
AfxMessageBox(buf);//这里可以正常显示buf的字符串值
int len=strlen(buf);
for(int i=0;i<len;i++){
CString str;
str.Format("%c",buf[i]);
AfxMessageBox(str);//会出现乱码,不知道为什么,请解释.
}
}
char* CLchQQTestDlg::GetAllData(){
char buf[MAX_PATH];
GetPrivateProfileSection("initest",(LPSTR)buf,MAX_PATH,(LPCSTR)".\\test.ini");
return buf;
}
大体意思就是在GetAllData函数内部显示的话就没有问题,但是如果接收GetAllData返回的指针再
显示的话就出现乱码.为什么会这样?