Advertisement
STANAANDREY

cp files

Jan 9th, 2023
3,069
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4.  
  5. FILE *safeOpenFile(const char *const path, const char *const mode) {
  6.     FILE *file = NULL;
  7.     file = fopen(path, mode);
  8.     if (file == NULL) {
  9.         perror("opening error!");
  10.         exit(EXIT_FAILURE);
  11.     }
  12.     return file;
  13. }
  14.  
  15. void safeCloseFile(FILE *file) {
  16.     if (fclose(file) == EOF) {
  17.         perror(NULL);
  18.     }
  19. }
  20.  
  21. void argCheck(int argc) {
  22.     if (argc < 3) {
  23.         fprintf(stderr, "MORE ARG(s) NEEDED!\n");
  24.         exit(-1);
  25.     }
  26. }
  27.  
  28.  
  29. int main(int argc, char **argv) {
  30.     argCheck(argc);
  31.     FILE *fin = safeOpenFile(argv[1], "rb");
  32.     FILE *fout = safeOpenFile(argv[2], "wb");
  33.  
  34.     for (uint8_t byte; fread(&byte, sizeof(byte), 1, fin) == 1;) {
  35.         fwrite(&byte, sizeof(byte), 1, fout);
  36.     }
  37.  
  38.     safeCloseFile(fin);
  39.     safeCloseFile(fout);
  40.     return 0;
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement