#include <windows.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hFile = CreateFile("one.dat", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
cout << "CreateFile is fail" << endl;
return 0;
}
DWORD dwFileSize = GetFileSize(hFile, NULL);
HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0,
dwFileSize + 5 * sizeof(char), "wsTest");
LPVOID lpView = NULL;
if (hFileMap)
{
lpView = MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, 0);
}
char *lpContent = (char*)lpView;
strcpy(lpContent + dwFileSize / sizeof(char), "hello");
UnmapViewOfFile(lpView);
CloseHandle(hFileMap);
CloseHandle(hFile);
cin.get();
return 0;
}
用内存映射的方式在文件末尾追加一个hello
最新推荐文章于 2021-07-10 09:47:55 发布