Win32 文件的读写操作
作者:互联网
https://www.cctry.com/thread-298355-1-1.html
文件的写操作:WriteFile:
函数的声明如下:
BOOL WriteFile(
HANDLE hFile,
LPCVOID lpBuffer,
DWORD nNumberOfBytesToWrite,
LPDWORD lpNumberOfBytesWritten,
LPOVERLAPPED lpOverlapped
);
函数详情见:https://www.cctry.com/thread-298326-1-1.html
实战:
接下来给大家讲解下如何向文件中写入如下类型的数据:
①、写入数字;
②、写入英文字母;
③、写入包含中文的字符串;
④、写入二进制数据。
文件的读操作:ReadFile:
函数的声明如下:
BOOL ReadFile(
HANDLE hFile,
LPVOID lpBuffer,
DWORD nNumberOfBytesToRead,
LPDWORD lpNumberOfBytesRead,
LPOVERLAPPED lpOverlapped
);
函数详情见:https://www.cctry.com/thread-298333-1-1.html
实战:
①、读入数字;
②、读入英文字母;
③、读入包含中文的字符串;
④、读入二进制数据
作业:
文本文件的内容为:23+3=
要求:读入文本文件中的表达式,并计算表达式的值,写入到文本文件中,让文本文件的内容为:23+3=26
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
HANDLE hFile = CreateFile(_T("D:\\VC驿站ok.txt"), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
MessageBox(NULL, _T("文件打开失败!"), _T("Tip"), MB_OK);
return -1;
}
DWORD dwWrited = 0;
wchar_t szbuf[] = _T("我是VC驿站的粉丝!");
static const BYTE unicodeHead[] = { 0xFF, 0xFE }; //unicode文件头文件
WriteFile(hFile, unicodeHead, sizeof(unicodeHead), &dwWrited, NULL);
BOOL bWriteOK = WriteFile(hFile, szbuf, sizeof(szbuf), &dwWrited, NULL);
CloseHandle(hFile);
return 0;
}
标签:文件,HANDLE,读写操作,写入,Win32,WriteFile,读入,NULL,hFile 来源: https://blog.csdn.net/PZ0605/article/details/95189480