Advertisement
NoamCohen123

Read_Write File

Jul 17th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <Windows.h>
  4. #include <strsafe.h>
  5. #include <tchar.h>
  6.  
  7. #define _UNICODE
  8. #define UNICODE
  9.  
  10. int _tmain(int argc, TCHAR *argv[])
  11. {
  12.     char *buffer;
  13.     if (argc == 4)
  14.     {
  15.  
  16.         char *p;
  17.         int number_of_bytes = strtol(argv[3], &p, 10);
  18.         printf("Number of bytes to read is %d", number_of_bytes);
  19.         printf("\n%s\n%s", argv[1], argv[2]);
  20.        
  21.         DWORD dwBytesWritten = 0;
  22.  
  23.    
  24.         buffer = (char*)malloc(number_of_bytes * sizeof(char));
  25.        
  26.  
  27.         HANDLE h = CreateFile(argv[1],               // file to open
  28.             GENERIC_READ,          // open for reading
  29.             0,       // share for reading
  30.             NULL,                  // default security
  31.             OPEN_EXISTING,         // existing file only
  32.             FILE_ATTRIBUTE_NORMAL, // normal file
  33.             NULL);                 // no attr. template
  34.         //DWORD a = GetLastError();
  35.         //if (a) { printf("\nWriting to the File error is: %d", a); }
  36.  
  37.         ReadFile(h, buffer, number_of_bytes, NULL, NULL);
  38.         DWORD c = GetLastError();
  39.         if (c) { printf("\nReading to the File error is: %d", c); }
  40.         CloseHandle(h);
  41.  
  42.  
  43.         DWORD dwBytesToWrite = (DWORD)strlen(buffer);
  44.  
  45.         //printf("%s\n\n", buffer);
  46.         //printf("The size of the buffer is %d", sizeof(buffer));
  47.  
  48.         HANDLE file_write = CreateFile(argv[2], GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  49.         DWORD last_error = GetLastError();
  50.         if (last_error){ printf("\nFile to write error is: %d", last_error); }
  51.        
  52.         WriteFile(file_write, buffer, dwBytesToWrite, &dwBytesWritten, NULL);
  53.         DWORD b = GetLastError();
  54.         if(b){ printf("\nWriting to the File error is: %d", b); }
  55.         CloseHandle(file_write);
  56.         scanf("%d");
  57.     }
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement