pastebin - collaborative debugging tool
nrubsig.kpaste.net RSS


winsetfilesize1.c - Win32 set file size
Posted by Anonymous on Wed 15th May 2024 23:45
raw | new post

  1.  
  2. /*
  3.  * winsetfilesize1.c - Win32 set file size
  4.  *
  5.  * Written by Roland Mainz <roland.mainz@nrubsig.org>
  6.  */
  7.  
  8. #define UNICODE 1
  9. #define _UNICODE 1
  10.  
  11. #include <windows.h>
  12. #include <stdlib.h>
  13. #include <stdbool.h>
  14. #include <stdio.h>
  15.  
  16. int main(int ac, char *av[]) {
  17.         HANDLE hFile = CreateFileA("myfile.txt", GENERIC_WRITE, 0, NULL,
  18.                             OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  19.  
  20.         if (hFile == INVALID_HANDLE_VALUE) {
  21.                 // Error handling
  22.                 (void)fprintf(stderr, "Error opening file! (%d)\n", GetLastError());
  23.                 return 1;
  24.         }
  25.  
  26.         LARGE_INTEGER newEndOfFile = { .QuadPart = 1234567890 };
  27.  
  28.         if (true) {
  29.                 FILE_END_OF_FILE_INFO endOfFile;
  30.                 endOfFile.EndOfFile = newEndOfFile;
  31.  
  32.                 if (!SetFileInformationByHandle(hFile, FileEndOfFileInfo,
  33.                         &endOfFile, sizeof(endOfFile))) {
  34.                         (void)fprintf(stderr, "FileEndOfFileInfo: Error setting end of file! (%d)\n", GetLastError());
  35.                         (void)CloseHandle(hFile);
  36.                         return 1;
  37.                 }
  38.         }
  39.  
  40.  
  41.         newEndOfFile.QuadPart = 72448;
  42.         if (true) {
  43.                 FILE_ALLOCATION_INFO endOfFile;
  44.                 endOfFile.AllocationSize = newEndOfFile;
  45.  
  46.                 if (!SetFileInformationByHandle(hFile, FileAllocationInfo,
  47.                         &endOfFile, sizeof(endOfFile))) {
  48.                         (void)fprintf(stderr, "FileAllocationInfo: Error setting end of file! (%d)\n", GetLastError());
  49.                         (void)CloseHandle(hFile);
  50.                         return 1;
  51.                 }
  52.         }
  53.  
  54.         // Close the file handle
  55.         (void)CloseHandle(hFile);
  56.  
  57.         (void)printf("End of file successfully set!\n");
  58.         return 0;
  59. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at