Skip to content

Instantly share code, notes, and snippets.

@FedericoPonzi
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FedericoPonzi/7609f571740060961d76 to your computer and use it in GitHub Desktop.
Save FedericoPonzi/7609f571740060961d76 to your computer and use it in GitHub Desktop.
File reverser usando le win32api
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
char s; //buffer
HANDLE file; //Handle del file
DWORD fileSize; //La dimensione del file
LPDWORD lpFileSizeHigh; //In caso la dimensione sia > 2gb
file = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE)
{
printf("Error opening the file: %x", GetLastError());
return 1;
}
fileSize = GetFileSize(file, &lpFileSizeHigh);
LPVOID lpBuffer = &s;
DWORD nBytesToRead, lpnBytesToRead;
nBytesToRead = 1;
SetFilePointer(file, -1L, NULL, SEEK_END);
while (fileSize > 0)
{
fileSize--;
if (!ReadFile(file, &lpBuffer, nBytesToRead, &lpnBytesToRead, NULL)) {
printf("Error on the Read: %d", GetLastError());
return 2;
}
printf("%c", lpBuffer);
SetFilePointer(file, -2L, NULL, SEEK_CUR);
}
if (!CloseHandle(file))
{
printf("Error, can't close the file: %x", GetLastError());
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment