VS2008, MFC 文件的操作3 - Win32 API 方式 文本方式打开
程序员文章站
2022-07-05 12:54:52
...
接上一节笔记:VS2008, MFC 文件的操作2 - C++ 语言方式 文本方式打开
1. 代码
void Cvs2008_SX_jiaocheng12View::OnFileWritefile()
{
// TODO: 在此添加命令处理程序代码
//Win32 API 方式
HANDLE hFile;
hFile = CreateFile(_T("4.txt"),GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD dwWrites;
WriteFile(hFile,"xxxxxxxxxxyyyyyy",strlen("xxxxxxxxxxyyyyyy"),&dwWrites,NULL);
CloseHandle(hFile);
}
void Cvs2008_SX_jiaocheng12View::OnFileReadfile()
{
// TODO: 在此添加命令处理程序代码
//Win32 API 方式
HANDLE hFile;//文件句柄
hFile = CreateFile(_T("4.txt"),GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
char ch[100];
DWORD dwReads;
ReadFile(hFile,ch,100,&dwReads,NULL);
ch[dwReads] = 0;
//char 和wchar 转换
int num = MultiByteToWideChar(0,0,ch,-1,NULL,0);
wchar_t *wide = new wchar_t[num];
MultiByteToWideChar(0,0,ch,-1,wide,num);
MessageBox(wide);
CloseHandle(hFile);
}