vc 改变文件大小

时间:2021-03-04 09:12:04
HANDLE fileHandle = CreateFile(_T("E:\\MyFile\\abc.txt"), GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ |FILE_SHARE_WRITE,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);


if (fileHandle == INVALID_HANDLE_VALUE)
{

std::cout<< "error Code :" << GetLastError() << std::endl;

return 1;

}

LARGE_INTEGER li;
li.QuadPart = 1024*2;
BOOL bRet = SetFilePointerEx(fileHandle, li, NULL, FILE_BEGIN);     // 设置文件指针的位置
SetEndOfFile(fileHandle);

CloseHandle(fileHandle);


// CFile 自动析构

CFile file;
if (!file.Open(_T("E:\\MyFile\\abc.txt"), CFile::modeReadWrite))
{
printf("Open file error");
return 1;
}

ULONGLONG le = file.GetLength();