Advertisement
I_LIKE_COFFEE

worked

Jan 19th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #define LEN 20
  6.  
  7.  
  8. void code(char* argv2, char* argv3, int shift) {//шифровка
  9. FILE* f_ptr1;
  10. FILE* f_ptr2;
  11. f_ptr1 = fopen(argv2, "r");
  12. f_ptr2 = fopen(argv3, "w");
  13. fseek(f_ptr1, 0, SEEK_END);
  14. long size = ftell(f_ptr1) + 1;
  15. fseek(f_ptr1, 0, SEEK_SET);
  16. char* str = (char*)malloc(size);
  17. fgets(str, (int)size, f_ptr1);
  18. printf("%s\n", str);
  19.  
  20. for (int i = 0; i < strlen(str); i++) {
  21. str[i] = str[i] + ((int)shift % 127);
  22. if (str[i] > 127) str[i] = 0 + (str[i] + 127) - 1;
  23. }
  24. printf("%s", str);
  25.  
  26.  
  27. fputs(str, f_ptr2);
  28. fclose(f_ptr1);
  29. fclose(f_ptr2);
  30. }
  31.  
  32. void decode(char* argv2, char* argv3, int shift) {//дешифровка
  33. FILE* f_ptr2;
  34. f_ptr2 = fopen(argv2, "r+");
  35. fseek(f_ptr2, 0, SEEK_END);
  36. long size = ftell(f_ptr2) + 1;
  37. fseek(f_ptr2, 0, SEEK_SET);
  38. char* dop = (char*)malloc(size);
  39. fgets(dop, size, f_ptr2);
  40. printf("\n%s", dop);
  41.  
  42.  
  43. for (int i = 0; i < strlen(dop); i++) {
  44. dop[i] = dop[i] - ((int)shift % 127);
  45. if (dop[i] < 0) dop[i] = 127 - (0 - dop[i]) + 1;
  46. }
  47.  
  48. printf("\n%s", dop);
  49. fclose(f_ptr2);
  50. f_ptr2 = fopen(argv3, "w");
  51. fputs(dop, f_ptr2);
  52. fclose(f_ptr2);
  53. }
  54.  
  55.  
  56. int main(int argc, char* argv[])
  57. {
  58. char* type = argv[1];
  59. char* first = argv[2];
  60. char* second = argv[3];
  61. char* res = argv[4];
  62. int shift = atoi(res);//аргументы кмд
  63.  
  64. if (*type == '1')
  65. code(first, second, shift);
  66. else if (*type == '0')
  67. decode(first, second, shift);
  68. else
  69. printf("Wrong command");
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement