16,472
社区成员
发帖
与我相关
我的任务
分享
void CFileView::OnFileWrite()
{
FILE* pFile = fopen("1.txt", "w");
char buf[22] = "http://www.sunxin.org";
buf[21] = '\0';
fwrite(buf, 1, 22, pFile);
fflush(pFile);
}
void CFileView::OnFileRead()
{
FILE* pFile = fopen("1.txt", "r");
char ch[100];
fread(ch, 1, 100, pFile);
fclose(pFile);
// error C2664: “CWnd::MessageBoxW”: 不能将参数 1 从“char [100]”转换为“LPCTSTR”
// 1> 与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
MessageBox(ch);
void CFileView::OnFileRead()
{
FILE* pFile = fopen("1.txt", "r");
char ch[100];
fread(ch, 1, 100, pFile);
fclose(pFile);
// error C2664: “CWnd::MessageBoxW”: 不能将参数 1 从“char [100]”转换为“LPCTSTR”
// 1> 与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
USE_CONVERSION;
CString strMsg = A2W(ch);
MessageBox(strMsg);
主要是使用USE_CONVERSION的一些现有功能进行编码转换。
需要注意的是,入门时候可以这么做,未来在实际使用的时候需要进一步弄清楚这个宏的一些限制。
另外,这个方法仅是其中的一个较容易入门的方法之一,还有一些更好的,但代码上比较麻烦一些的,在入门之后,以后可以慢慢接触更多更“标准”的方法
MessageBoxA(ch);
转换编码就没必要了,messagebox一般只作提示,排错用.
void CFileView::OnFileWrite()
{
FILE* pFile = _tfopen(_T("1.txt"), _T("wb"));
TCHAR buf[22] =_T("http://www.sunxin.org");
buf[21] = 0;
fwrite(buf, 1, 22*sizeof(TCHAR), pFile);
fclose(pFile);
}
void CFileView::OnFileRead()
{
FILE* pFile = _tfopen(_T("1.txt"),_T("rb"));
TCHAR ch[100];
fread(ch, 1, 100*sizeof(TCHAR), pFile);
fclose(pFile);
}