Guest User

Untitled

a guest
Mar 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <windows.h>
  4.  
  5. #define DATA_SIZE 256
  6.  
  7. int main(int argc, char **argv)
  8. {
  9. HANDLE hTestFile; // File handle
  10. LPCTSTR lpszPathToFile; // Path to file from argument argv[1]
  11. LPCTSTR lpszFileString; // String that we will write to file
  12. BOOL bIOresult; // File input/output result
  13. DWORD cbIObytes; // Amount of read/wrote bytes
  14. TCHAR cReadData[DATA_SIZE]; // Memory for read data
  15. DWORD dwSetPtr; // Value returned by SetFilePointer()
  16.  
  17. if(argc < 2)
  18. {
  19. printf("Enter a file name and any string for file input/output\n");
  20.  
  21. return -1;
  22. }
  23.  
  24. lpszPathToFile = (LPCTSTR)argv[1];
  25. lpszFileString = (LPCTSTR)argv[2];
  26. memset(cReadData, 0x0, DATA_SIZE);
  27.  
  28. /* Open file from a command line argument */
  29. hTestFile = CreateFile(lpszPathToFile,
  30. GENERIC_READ|GENERIC_WRITE,
  31. 0,
  32. NULL,
  33. CREATE_ALWAYS,
  34. FILE_ATTRIBUTE_NORMAL,
  35. NULL);
  36.  
  37. if(hTestFile == INVALID_HANDLE_VALUE)
  38. {
  39. printf("Can't create the file \"%s\", code %d\n", lpszPathToFile, (int)GetLastError());
  40.  
  41. return -2;
  42. }
  43.  
  44. printf("File \"%s\" is opened\n\n", lpszPathToFile);
  45.  
  46. /* Write any data in the file */
  47. bIOresult = WriteFile(hTestFile, // file handle
  48. lpszFileString, // data file
  49. strlen(lpszFileString), // data file length
  50. &cbIObytes, // input amount of bytes in fact
  51. NULL); // file access: structure OVERLAPPED - asynchronous, NULL - synchronous
  52.  
  53. if(bIOresult == FALSE)
  54. {
  55. printf("Can't write data in file %s, code %d\n", lpszPathToFile, (int)GetLastError());
  56.  
  57. return -3;
  58. }
  59.  
  60. printf("Wrote %d bytes successfully\n", (int)cbIObytes);
  61. printf("\tDATA: %s\n\n", lpszFileString);
  62.  
  63. dwSetPtr = SetFilePointer(hTestFile,
  64. 0,
  65. NULL,
  66. FILE_BEGIN);
  67.  
  68. if(dwSetPtr == INVALID_SET_FILE_POINTER)
  69. {
  70. printf("Can't set pointer on start of the file for reading\n");
  71.  
  72. return -4;
  73. }
  74.  
  75. /* Currently we read wrote bytes from file and output it to console */
  76. bIOresult = ReadFile(hTestFile,
  77. cReadData,
  78. DATA_SIZE,
  79. &cbIObytes,
  80. NULL);
  81.  
  82. if(bIOresult == FALSE)
  83. {
  84. printf("Can't read data from file \"%s\", code %d\n", lpszPathToFile, (int)GetLastError());
  85.  
  86. return -5;
  87. }
  88.  
  89. printf("Read %d bytes successfully:\n", (int)cbIObytes);
  90. printf("\tDATA: %s\n\n", cReadData);
  91.  
  92. /* Close the opened file */
  93. if(!CloseHandle(hTestFile))
  94. {
  95. printf("Can't close the file \"%s\", code %d\n", lpszPathToFile, (int)GetLastError());
  96.  
  97. return -6;
  98. }
  99.  
  100. printf("File \"%s\" is closed\n\n", lpszPathToFile);
  101.  
  102. /* Remove the file */
  103. if(!DeleteFile(lpszPathToFile))
  104. printf("Can't delete file \"%s\", code %d\n", lpszPathToFile, (int)GetLastError());
  105.  
  106. printf("File \"%s\" is successfully removed\n\n", lpszPathToFile);
  107.  
  108. return 0;
  109. }
Add Comment
Please, Sign In to add comment