Advertisement
DrColonelM

Untitled

May 9th, 2021
1,202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.69 KB | None | 0 0
  1. /*********************************
  2. * Class: MAGSHIMIM C2            *
  3. * Name:  Itay                    *
  4. **********************************/
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #define NUM_OF_ARGS 4
  11. #define MODE_PARAM (*(argv + 1))
  12. #define SRC_PARAM (*(argv + 2))
  13. #define DST_PARAM (*(argv + 3))
  14.  
  15. #define MODE_TXT 1
  16. #define MODE_BIN 2
  17.  
  18. #define FALSE 0
  19. #define TRUE 1
  20.  
  21.  
  22. int fileExists(char* fileName)
  23. {
  24.     FILE* pf = 0;
  25.  
  26.     if (pf = fopen(fileName, "r"))
  27.     {
  28.         fclose(pf);
  29.         return TRUE;
  30.     }
  31.  
  32.     return FALSE;
  33. }
  34.  
  35.  
  36. void checkInput(int argc, char** argv)
  37. {
  38.     if (argc != NUM_OF_ARGS) // if there are not enough args
  39.     {
  40.         printf("Bad arguments!\n");
  41.        
  42.         printf("Press Enter to continue...\n");
  43.         getchar();
  44.         exit(0);
  45.     }
  46.  
  47.     if (strcmp(MODE_PARAM, "textCopy") && strcmp(MODE_PARAM, "binaryCopy")) // if the first param is not a valid action
  48.     {
  49.         printf("Bad arguments!\n");
  50.  
  51.         printf("Press Enter to continue...\n");
  52.         getchar();
  53.         exit(0);
  54.     }
  55.  
  56.     if (!fileExists(SRC_PARAM))
  57.     {
  58.         printf("Source file does not exist!\n");
  59.  
  60.         printf("Press Enter to continue...\n");
  61.         getchar();
  62.         exit(0);
  63.     }
  64. }
  65.  
  66.  
  67. void initializeFiles(int argc, char** argv, FILE** in, FILE** out, int mode)
  68. {
  69.     int choice = 0;
  70.  
  71.     if (!fileExists(SRC_PARAM)) // if the file does not exist, tell the user and exit
  72.     {
  73.         printf("%s file does not exist.\n", SRC_PARAM);
  74.         getchar();
  75.  
  76.         printf("Press Enter to continue...\n");
  77.         getchar();
  78.         exit(0);
  79.     }
  80.  
  81.     *in = fopen(SRC_PARAM, mode == MODE_TXT ? "r" : "rb");
  82.  
  83.     if (fileExists(DST_PARAM)) // if the file exist, ask the user what to do
  84.     {
  85.         printf("Do you want to overwrite? 0 (no) / 1 (yes)\n");
  86.         scanf("%d", &choice);
  87.         getchar();
  88.  
  89.         if (choice == FALSE)
  90.         {
  91.             fclose(*in);
  92.  
  93.             printf("Press Enter to continue...\n");
  94.             getchar();
  95.             exit(0);
  96.         }
  97.     }
  98.  
  99.     *out = fopen(DST_PARAM, mode == MODE_TXT ? "w" : "wb"); // if user want to overwrite open a new file
  100. }
  101.  
  102.  
  103. void copyTxt(FILE* src, FILE* dst)
  104. {
  105.     char ch = 0;
  106.  
  107.     while ((ch = fgetc(src)) != EOF)
  108.     {
  109.         fputc(ch, dst);
  110.     }
  111. }
  112.  
  113.  
  114. void copyBin(FILE* src, FILE* dst)
  115. {
  116.     char byte[1] = { 0 };
  117.  
  118.     while (!feof(src))
  119.     {
  120.         fread(byte, sizeof(char), 1, src);
  121.         fwrite(byte, sizeof(char), 1, dst);
  122.     }
  123. }
  124.  
  125.  
  126. int main(int argc, char** argv)
  127. {
  128.     FILE* fsrc = 0;
  129.     FILE* fdst = 0;
  130.  
  131.     checkInput(argc, argv); // exits the program if input is not valid
  132.     initializeFiles(argc, argv, &fsrc, &fdst, !strcmp(MODE_PARAM, "textCopy") ? MODE_TXT : MODE_BIN);
  133.  
  134.     !strcmp(MODE_PARAM, "textCopy") ? copyTxt(fsrc, fdst) : copyBin(fsrc, fdst); // call copyTxt() or copyBin() based on action
  135.  
  136.     fclose(fsrc);
  137.     fclose(fdst);
  138.  
  139.     printf("Press Enter to continue...\n");
  140.     getchar();
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement