Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define LEN 20
- void code(char* argv2, char* argv3, int shift) {//шифровка
- FILE* f_ptr1;
- FILE* f_ptr2;
- f_ptr1 = fopen(argv2, "r");
- f_ptr2 = fopen(argv3, "w");
- fseek(f_ptr1, 0, SEEK_END);
- long size = ftell(f_ptr1) + 1;
- fseek(f_ptr1, 0, SEEK_SET);
- char* str = (char*)malloc(size);
- fgets(str, (int)size, f_ptr1);
- printf("%s\n", str);
- for (int i = 0; i < strlen(str); i++) {
- str[i] = str[i] + ((int)shift % 127);
- if (str[i] > 127) str[i] = 0 + (str[i] + 127) - 1;
- }
- printf("%s", str);
- fputs(str, f_ptr2);
- fclose(f_ptr1);
- fclose(f_ptr2);
- }
- void decode(char* argv2, char* argv3, int shift) {//дешифровка
- FILE* f_ptr2;
- f_ptr2 = fopen(argv2, "r+");
- fseek(f_ptr2, 0, SEEK_END);
- long size = ftell(f_ptr2) + 1;
- fseek(f_ptr2, 0, SEEK_SET);
- char* dop = (char*)malloc(size);
- fgets(dop, size, f_ptr2);
- printf("\n%s", dop);
- for (int i = 0; i < strlen(dop); i++) {
- dop[i] = dop[i] - ((int)shift % 127);
- if (dop[i] < 0) dop[i] = 127 - (0 - dop[i]) + 1;
- }
- printf("\n%s", dop);
- fclose(f_ptr2);
- f_ptr2 = fopen(argv3, "w");
- fputs(dop, f_ptr2);
- fclose(f_ptr2);
- }
- int main(int argc, char* argv[])
- {
- char* type = argv[1];
- char* first = argv[2];
- char* second = argv[3];
- char* res = argv[4];
- int shift = atoi(res);//аргументы кмд
- if (*type == '1')
- code(first, second, shift);
- else if (*type == '0')
- decode(first, second, shift);
- else
- printf("Wrong command");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement