Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4. void main()
  5. {
  6. HANDLE hFile;
  7. HANDLE hAppend;
  8. DWORD dwBytesRead, dwBytesWritten, dwPos;
  9. BYTE buff[4096];
  10.  
  11. // Copy the existing file.
  12.  
  13. BOOL bCopy = CopyFile(TEXT("1.txt"), TEXT("3.txt"), TRUE);
  14. if (!bCopy)
  15. {
  16. printf("Could not copy 1.txt to 3.txt.");
  17. return;
  18. }
  19.  
  20. // Open the existing file.
  21.  
  22. hFile = CreateFile(TEXT("2.txt"), // open 2.txt
  23. GENERIC_READ, // open for reading
  24. 0, // do not share
  25. NULL, // no security
  26. OPEN_EXISTING, // existing file only
  27. FILE_ATTRIBUTE_NORMAL, // normal file
  28. NULL); // no attr. template
  29.  
  30. if (hFile == INVALID_HANDLE_VALUE)
  31. {
  32. printf("Could not open 2.txt.");
  33. return;
  34. }
  35.  
  36. // Open the existing file, or if the file does not exist,
  37. // create a new file.
  38.  
  39. hAppend = CreateFile(TEXT("3.txt"), // open 3.txt
  40. FILE_APPEND_DATA, // open for writing
  41. FILE_SHARE_READ, // allow multiple readers
  42. NULL, // no security
  43. OPEN_ALWAYS, // open or create
  44. FILE_ATTRIBUTE_NORMAL, // normal file
  45. NULL); // no attr. template
  46.  
  47. if (hAppend == INVALID_HANDLE_VALUE)
  48. {
  49. printf("Could not open 3.txt.");
  50. return;
  51. }
  52.  
  53. // Append the first file to the end of the second file.
  54. // Lock the second file to prevent another process from
  55. // accessing it while writing to it. Unlock the
  56. // file when writing is complete.
  57.  
  58. while (ReadFile(hFile, buff, sizeof(buff), &dwBytesRead, NULL)
  59. && dwBytesRead > 0)
  60. {
  61. dwPos = SetFilePointer(hAppend, 0, NULL, FILE_END);
  62. LockFile(hAppend, dwPos, 0, dwBytesRead, 0);
  63. WriteFile(hAppend, buff, dwBytesRead, &dwBytesWritten, NULL);
  64. UnlockFile(hAppend, dwPos, 0, dwBytesRead, 0);
  65. }
  66.  
  67. // Close both files.
  68.  
  69. CloseHandle(hFile);
  70. CloseHandle(hAppend);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement