Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 22nd, 2012  |  syntax: None  |  size: 2.67 KB  |  views: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7.  
  8. FILE *openFile(char filename[]);
  9. FILE *createFile(char filename[]);
  10. void printUsage();
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14.     FILE *source; /* source file */
  15.     FILE *dist; /* destination file */
  16.     char arr[500]; /* a place to store lines from files */
  17.     int i=0;
  18.    
  19.     /* if wrong number of arguments passed */
  20.     if (argc != 3)
  21.         printUsage(argv[0]);
  22.    
  23.     else {
  24.         source = openFile(argv[1]);
  25.         dist = createFile(argv[2]);
  26.        
  27.         /* scan each line and apply the cipher */
  28.         while ((fgets(arr, 500, source)) != NULL) {
  29.             for (i = 0; i < strlen(arr); i++)
  30.                 if (isalpha(arr[i]) && islower(arr[i]))
  31.                     arr[i] = (arr[i] + 13) > 'z' ? arr[i] - 13 : arr[i] + 13;
  32.                 else if (isalpha(arr[i]) && isupper(arr[i]))
  33.                     arr[i] = (arr[i] + 13) > 'Z' ? arr[i] - 13 : arr[i] + 13;
  34.  
  35.             fputs(arr, dist);
  36.         }
  37.     }
  38.     fclose(source);
  39.     fclose(dist);
  40.    
  41.     return EXIT_SUCCESS;
  42. }
  43.  
  44. void printUsage(char programName[])
  45. {
  46.     printf("USAGE: %s <filename1.txt> <filename2.txt>\n\n", programName);
  47.     exit(1);
  48. }
  49.  
  50. FILE *openFile(char filename[]){
  51.     FILE *fp;
  52.     fp = fopen(filename, "r");
  53.     if (fp == NULL) {
  54.         printf("\nERROR: file \"%s\" does not exist.\n", filename);
  55.         exit(1);
  56.     }
  57.     else
  58.         return fp;
  59. }
  60.  
  61. FILE *createFile(char filename[]){
  62.     FILE *fp;
  63.     char response;
  64.    
  65.     /* check to see if distination file already exists */
  66.     fp = fopen(filename, "r");
  67.    
  68.     if (fp != NULL) {
  69.         while(1) {
  70.             printf("Destination file already exits;");
  71.             printf(" overwrite? [y/n] ");
  72.             scanf("%c", &response);
  73.            
  74.             if ((tolower(response)) == 'y'){
  75.                 fclose(fp);
  76.                 fp = fopen(filename, "w");
  77.                 return fp;
  78.             }
  79.            
  80.             else if ((tolower(response)) == 'n')
  81.                 exit(1);
  82.  
  83.             else {
  84.                 printf("invalid response.\n");
  85.                 scanf("%c", &response);
  86.                 /* this line picks up the '\n' so it doesn't
  87.                 get re-scanned in the next loop */
  88.             }
  89.         }
  90.     }
  91.    
  92.     /* if file doesn't exist, create it */
  93.     else {
  94.         fp = fopen(filename, "w");
  95.         if (fp == NULL) {
  96.             printf("Unable to create destination file. make");
  97.             printf(" sure you have proper permissions.\n");
  98.             exit(1);
  99.         }
  100.         else
  101.             return fp;
  102.     }
  103. }
  104. // 'a' + (c-'a')%('z'-'a'+1)