Advertisement
Yonka2019

question3

May 12th, 2021
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.65 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. #define BUFFERSIZE 512
  7.  
  8. /*
  9. *  Program Arguments:
  10. *  program.exe [textCopy|binaryCopy] file.src file.dst
  11. */
  12.  
  13. void copy(char* method, char* src, char* dst);
  14. int copyTextFile(char* src, char* dst);
  15. int copyBinaryFile(char* src, char* dst);
  16. int fileExists(char* filename);
  17.  
  18. int main(int argc, char** argv)
  19. {
  20.     if (argc == 4) // check for 4 args
  21.     {
  22.         /*
  23.         * argv[0] = programName
  24.         * argv[1] = textCopy/binaryCopy/..
  25.         * argv[2] = file.src
  26.         * argv[3] = file.dst
  27.         */
  28.  
  29.         if (fileExists(argv[2])) // Check if file.src exists
  30.         {
  31.             if (fileExists(argv[3])) // Chec if file.dst already exists (overwrite request)
  32.             {
  33.                 int overwrite = 0;
  34.  
  35.                 printf("Do you want to overwrite? 0 (no) / 1 (yes)\n");
  36.                 scanf("%d", &overwrite);
  37.                 getchar();
  38.  
  39.                 if (overwrite)
  40.                 {
  41.                     copy(argv[1], argv[2], argv[3]);
  42.                 }
  43.             }
  44.             else
  45.             {
  46.                 copy(argv[1], argv[2], argv[3]);
  47.             }
  48.         }
  49.         else
  50.         {
  51.             printf("%s file does not exist\n", argv[2]);
  52.         }
  53.     }
  54.     else
  55.     {
  56.         printf("Invalid execution\n"
  57.             "Usage: copyFile (textCopy|binaryCopy) <sourceFilename_filename> <destFilenameination_filename>\n");
  58.     }
  59. }
  60. /// <summary>
  61. /// Checks if the given file already exists
  62. /// </summary>
  63. /// <param name="filename">File to check if he exists</param>
  64. /// <returns>1 - If exists, 0 - If not exists</returns>
  65. int fileExists(char* filename)
  66. {
  67.     FILE* file = NULL;
  68.  
  69.     if (file = fopen(filename, "r"))
  70.     {
  71.         fclose(file);
  72.         return 1;
  73.     }
  74.  
  75.     return 0;
  76. }
  77. /// <summary>
  78. /// Copys SRC file to DST file
  79. /// </summary>
  80. /// <param name="src">Source file</param>
  81. /// <param name="dst">Destination</param>
  82. /// <returns>1 - if success, 0 - if something went wrong..</returns>
  83. int copyTextFile(char* src, char* dst)
  84. {
  85.     FILE* srcFile = NULL;
  86.     FILE* dstFile = NULL;
  87.     char ch = ' ';
  88.  
  89.     srcFile = fopen(src, "r");
  90.     if (srcFile == NULL)
  91.     {
  92.         printf("[ERROR] Can't open %s file.", src);
  93.  
  94.         return 0;
  95.     }
  96.  
  97.     dstFile = fopen(dst, "w");
  98.     if (dstFile == NULL)
  99.     {
  100.         fclose(srcFile);
  101.         printf("[ERROR] Can't open %s file", dst);
  102.  
  103.         return 0;
  104.     }
  105.  
  106.     while ((ch = fgetc(srcFile)) != EOF)
  107.     {
  108.         fputc(ch, dstFile);
  109.     }
  110.  
  111.     fclose(srcFile);
  112.     fclose(dstFile);
  113.  
  114.     return 1;
  115. }
  116. /// <summary>
  117. /// Copys SRC file to DST file
  118. /// </summary>
  119. /// <param name="src">Source file</param>
  120. /// <param name="dst">D</param>
  121. /// <returns></returns>
  122. int copyBinaryFile(char* src, char* dst)
  123. {
  124.     char buffer[BUFFERSIZE] = { 0 };
  125.     FILE* srcFile = NULL;
  126.     FILE* dstFile = NULL;
  127.     size_t bytes = 0;
  128.  
  129.     srcFile = fopen(src, "rb");
  130.     if (srcFile == NULL)
  131.     {
  132.         printf("[ERROR] Can't open %s file.", src);
  133.  
  134.         return 0;
  135.     }
  136.  
  137.     dstFile = fopen(dst, "wb");
  138.     if (dstFile == NULL)
  139.     {
  140.         fclose(srcFile);
  141.         printf("[ERROR] Can't open %s file", dst);
  142.  
  143.         return 0;
  144.     }
  145.  
  146.     while ((bytes = fread(buffer, 1, BUFFERSIZE, srcFile)) != 0) {
  147.         if (fwrite(buffer, 1, bytes, dstFile) != bytes) {
  148.  
  149.             return 0;
  150.         }
  151.     }
  152.  
  153.     fclose(srcFile);
  154.     fclose(dstFile);
  155.  
  156.     return 1;
  157. }
  158. /// <summary>
  159. /// Choosing the method to copy the given files
  160. /// </summary>
  161. /// <param name="method">Binary Copy / Text Copy</param>
  162. /// <param name="src">Source file</param>
  163. /// <param name="dst">Destination File</param>
  164. void copy(char* method, char* src, char* dst)
  165. {
  166.     if (!strcmp(method, "textCopy"))
  167.     {
  168.         if (copyTextFile(src, dst))
  169.         {
  170.             printf("Copy completed");
  171.         }
  172.     }
  173.     else if (!strcmp(method, "binaryCopy"))
  174.     {
  175.         if (copyBinaryFile(src, dst))
  176.         {
  177.             printf("Copy completed");
  178.         }
  179.     }
  180.     else
  181.     {
  182.         printf("Invalid execution\n"
  183.             "Usage: copyFile (textCopy|binaryCopy) <sourceFilename_filename> <destFilenameination_filename>\n");
  184.     }
  185. }
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement