fork download
  1. /* Chapter 1. Basic cp file copy program. Win32 Implementation. */
  2. /* cpW file1 file2: Copy file1 to file2. */
  3.  
  4. #include <windows.h>
  5. #include <stdio.h>
  6. #include <assert.h>
  7. #define BUF_SIZE 16384 /* Optimal in several experiments. Small values such as 256 give very bad performance */
  8.  
  9. int main(int argc, LPTSTR argv[])
  10. {
  11. HANDLE hIn, hOut;
  12. DWORD nIn, nOut;
  13. CHAR buffer[BUF_SIZE] = {0};
  14. if (argc != 3) {
  15. fprintf(stderr, "Usage: cp file1 file2\n");
  16. return 1;
  17. }
  18. hOut = CreateFile(argv[2], GENERIC_WRITE, 0, NULL,
  19. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  20. if (hOut == INVALID_HANDLE_VALUE) {
  21. fprintf(stderr, "Cannot open output file. Error: %x\n", GetLastError());
  22. return -1;
  23. }
  24. WriteFile(hOut, buffer, 1, &nOut, NULL);
  25. assert(nOut == 1);
  26. CloseHandle(hOut);
  27. hIn = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL,
  28. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  29. if (hIn == INVALID_HANDLE_VALUE) {
  30. fprintf(stderr, "Cannot open input file. Error: %x\n", GetLastError());
  31. return 2;
  32. }
  33.  
  34. hOut = CreateFile(argv[2], GENERIC_WRITE, 0, NULL,
  35. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  36. if (hOut == INVALID_HANDLE_VALUE) {
  37. fprintf(stderr, "Cannot open output file. Error: %x\n", GetLastError());
  38. CloseHandle(hIn);
  39. return 3;
  40. }
  41. while (ReadFile(hIn, buffer, BUF_SIZE, &nIn, NULL) && nIn > 0) {
  42. WriteFile(hOut, buffer, nIn, &nOut, NULL);
  43. if (nIn != nOut) {
  44. fprintf(stderr, "Fatal write error: %x\n", GetLastError());
  45. CloseHandle(hIn); CloseHandle(hOut);
  46. return 4;
  47. }
  48. }
  49. CloseHandle(hIn);
  50. CloseHandle(hOut);
  51. return 0;
  52. }
  53.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:4:10: fatal error: windows.h: No such file or directory
 #include <windows.h>
          ^~~~~~~~~~~
compilation terminated.
stdout
Standard output is empty