Advertisement
Guest User

Untitled

a guest
May 21st, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1. sys
  2.  
  3.  
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6.  
  7. #define BUFSIZE 512
  8. #define PERM    0644
  9.  
  10. /* funkcja kopiujaca */
  11. int copyfile(const char *plik1, const char *plik2)
  12. {
  13.     int infile, outfile;
  14.     ssize_t nread;
  15.     char buffer[BUFSIZE];
  16.  
  17.     if((infile = open(plik1, O_RDONLY)) == -1)
  18.     return(-1);
  19.  
  20.     if((outfile = open(plik2, O_WRONLY|O_CREAT|O_TRUNC, PERM)) == -1)
  21.     {
  22.     close(infile);
  23.     return(-2);
  24.     }  
  25.  
  26.     /*teraz czytaj z plik1 po BUFSIZE znakow naraz*/
  27.     while((nread = read(infile, buffer, BUFSIZE))>0)
  28.     {
  29.     /*zapisz bufor do ploku wyjsciowego*/
  30.     write(outfile, buffer, nread);
  31.     }
  32.     close(infile);
  33.     close(outfile);
  34.  
  35. }
  36. /////////////////////////////
  37. int main(int argc, char** argv)
  38. {
  39.     if (argc<3)
  40.     {
  41.      printf("Uzyj: %s plik_zrodlo plik_celn",argv[0]);
  42.      exit(1);
  43.     }
  44.  
  45.     copyfile(argv[1],argv[2]);
  46. }
  47.  
  48.  
  49. bibl
  50.  
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53.  
  54. /*funkcja kopiujaca*/
  55. int copyfile(const char *plik1, const char *plik2)
  56. {
  57.     FILE *in, *out;
  58.     int c;
  59.  
  60.     if ((in = fopen(plik1,"r")) == NULL)
  61.     return(-1);
  62.  
  63.     if ((out = fopen(plik2,"w")) == NULL)
  64.     {
  65.     fclose(in);
  66.     return(-2);
  67.     }      
  68.  
  69.     while((c = getc(in)) != EOF)
  70.     putc(c, out);
  71.  
  72.     fclose(in);
  73.     fclose(out);    
  74. }
  75. ////////////////////////////////
  76.  int main(int argc, char** argv)
  77.  {
  78.      if(argc<3)
  79.      {
  80.          printf("Uzyj: %s plik_zrodlo plik_celn",argv[0]);
  81.      exit(1);
  82.      }
  83.  
  84.      copyfile(argv[1],argv[2]);
  85.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement