Advertisement
Yonka2019

copyBinaryFile

May 12th, 2021
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.66 KB | None | 0 0
  1. int copyBinaryFile(char* src, char* dst)
  2. {
  3.     char buffer[BUFFERSIZE] = { 0 };
  4.     FILE* srcFile = NULL;
  5.     FILE* dstFile = NULL;
  6.     size_t bytes = 0;
  7.  
  8.     srcFile = fopen(src, "rb");
  9.     if (srcFile == NULL)
  10.     {
  11.         printf("[ERROR] Can't open %s file.", src);
  12.  
  13.         return 0;
  14.     }
  15.  
  16.     dstFile = fopen(dst, "wb");
  17.     if (dstFile == NULL)
  18.     {
  19.         fclose(srcFile);
  20.         printf("[ERROR] Can't open %s file", dst);
  21.  
  22.         return 0;
  23.     }
  24.  
  25.     while ((bytes = fread(buffer, 1, BUFFERSIZE, srcFile)) != 0) {
  26.         if (fwrite(buffer, 1, bytes, dstFile) != bytes) {
  27.             return 0;                                           // or other action
  28.         }
  29.     }
  30.  
  31.     fclose(srcFile);
  32.     fclose(dstFile);
  33.  
  34.     return 1;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement