1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
const char *fname = "SetEndOfFile_.test";
void error(const char *errMsg)
{
printf("ERROR: %s\nERROR CODE: %lu", errMsg, GetLastError());
getch();
exit(EXIT_FAILURE);
}
int main()
{
HANDLE hFile;
const char buff[] = "0123456789"; // 10 bytes
DWORD bytesWritten;
long whoa;
hFile = CreateFile(fname,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL); // kimi o ai shiteru, fopen. <3
if(hFile == INVALID_HANDLE_VALUE)
error("CreateFile failed");
if(!WriteFile(hFile, buff, sizeof(buff)-1, &bytesWritten, NULL)) //fwrite
error("WriteFile failed");
if(!SetFilePointer(hFile, 5L, &whoa, FILE_BEGIN)) //Set file pointer to move forward by 5 bytes.
error("SetFilePointer failed.");
if(!SetEndOfFile(hFile)) // hmmmm......
error("SetEndOfFile failed.");
CloseHandle(hFile);
getch();
return 0;
}
|