1、使用 SetFilePointer 将 数字 123 写入到 C:\Windows\notepad.exe 文件的结尾,之后再读进来(为了不破坏系统文件,可以将 C:\Windows\notepad.exe 文件拷贝到 D盘中);
2、学习实用 SetFilePointerEx API 函数;
3、在 C 盘根目录下,创建一个名字是“VC驿站”的文件夹;
4、封装自定义函数,实现可以根据参数创建多级子目录的文件夹,例如:创建:C:\VC驿站\cctry.com\编程学习
5、删除C盘根目录下的名字叫做 垃圾 的文件夹;
#define _CRT_SECURE_NO_WARNINGS #include <Windows.h> #include <tchar.h> #include <Shlwapi.h> #pragma comment(lib,"Shlwapi.lib") BOOL Write_File_Point(LPCTSTR lpFilePath) { HANDLE hFile = CreateFile(lpFilePath, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) return FALSE; DWORD hFileSize = GetFileSize(hFile,NULL); SetFilePointer(hFile, hFileSize,NULL,FILE_BEGIN); DWORD Real_Size = 0; int num = 123; BOOL bRet = WriteFile(hFile, &num, sizeof(int), &Real_Size,NULL); CloseHandle(hFile); if (!bRet) return FALSE; hFile = CreateFile(lpFilePath,GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) return FALSE; int ReadRet = 0; int size = sizeof(int); SetFilePointer(hFile, -size, NULL, FILE_END); bRet = ReadFile(hFile,&ReadRet,sizeof(int), &Real_Size, NULL); if (!bRet) return FALSE; CloseHandle(hFile); return TRUE; } BOOL Write_File_PointEx(LPCTSTR lpFilePath) { HANDLE hFile = CreateFile(lpFilePath, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) return FALSE; LONGLONG llFileSize = GetFileSize(hFile, NULL); LARGE_INTEGER ToMove_Size = {0}; ToMove_Size.QuadPart = llFileSize; SetFilePointerEx(hFile, ToMove_Size, NULL, FILE_BEGIN); DWORD Real_Size = 0; int num = 123; BOOL bRet = WriteFile(hFile, &num, sizeof(int), &Real_Size, NULL); CloseHandle(hFile); if (!bRet) return FALSE; hFile = CreateFile(lpFilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) return FALSE; int ReadRet = 0; ToMove_Size = { 0 }; int MoveSize = ~sizeof(int) + 1;//取反加1;例如a = 5;a = ~a + 1;把正数转换成负数 sdl可以开启 ToMove_Size.QuadPart = MoveSize; SetFilePointerEx(hFile, ToMove_Size, NULL, FILE_END); bRet = ReadFile(hFile, &ReadRet, sizeof(int), &Real_Size, NULL); if (!bRet) return FALSE; CloseHandle(hFile); return TRUE; } BOOL CreateDeepDirectory(LPCTSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes) { if (PathFileExists(lpPathName)) return TRUE; TCHAR szPath[MAX_PATH] = { 0 }; TCHAR pszSrc[MAX_PATH] = { 0 }; _tcscpy(pszSrc, lpPathName); TCHAR* pToken = _tcstok(pszSrc, _T("\\")); while (pToken) { _tcscat(szPath, pToken); _tcscat(szPath, _T("\\")); if (!PathFileExists(szPath)) { if (!CreateDirectory(szPath, lpSecurityAttributes)) return FALSE; } pToken = _tcstok(NULL, _T("\\")); } return TRUE; } int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, INT nCmdShow) { //BOOL bRet = Write_File_Point(_T("D:\\notepad.exe")); //BOOL bRet = Write_File_PointEx(_T("D:\\notepad.exe")); //BOOL bRet = CreateDirectory(_T("D:\\VC驿站"), NULL); //BOOL bRet = CreateDeepDirectory(_T("D:\\VC驿站\\cctry.com\\编程学习"),NULL); BOOL bRet = RemoveDirectory(_T("D:\\垃圾")); return 0; }