Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4.  
  5. int main (int argc, char *argv[])
  6. {
  7.     int choice; //command line element 'i', 'o' or 's' interpreted by getopt
  8.     char *ifilename;
  9.         char *ofilename;
  10.     int seed; //stores seed for rand()
  11.    
  12.    
  13.     extern char *optarg;
  14.     extern int optind, optopt;
  15.    
  16.    
  17.    
  18.     while((choice = getopt(argc, argv, ":i:o:s:")) != -1)
  19.     //sets variables according to command line arguments
  20.     {
  21.         switch(choice)
  22.         {
  23.         case 'i':
  24.             ifilename = optarg;
  25.             break;
  26.         case 'o':
  27.             ofilename = optarg;
  28.             break;
  29.         case 's':
  30.             seed = atoi(optarg);
  31.             break;
  32.         case ':': //missing operands
  33.             printf("-%c missing operand\n", optopt);
  34.         case '?': //invalid argument
  35.             printf("Invalid argument: -%c\n", optopt);
  36.         }
  37.     }
  38.    
  39.    
  40.    
  41.     FILE *ifile; //file to be encrypted
  42.     FILE *ofile; //storage of encrypted file
  43.    
  44.    
  45.     ifile = fopen(ifilename, "r");
  46.     ofile = fopen(ofilename, "w");
  47.    
  48.    
  49.    
  50.     srand(seed);
  51.         int key = rand();
  52.      
  53.         int c;
  54.    
  55.     while((c = fgetc(ifile)) != EOF)
  56.     {
  57.         if(ferror(ifile))
  58.         {
  59.             printf("Read error\n");
  60.             clearerr(ifile);
  61.             break;
  62.         }
  63.        
  64.         else
  65.             fputc(c ^ key, ofile);
  66.            
  67.         if (ferror(ofile))
  68.         {
  69.             printf("Write error\n");
  70.             clearerr(ofile);
  71.             break;
  72.         }      
  73.     }
  74.    
  75.    
  76.     fclose(ifile);
  77.     fclose(ofile);
  78.    
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement