Untitled
unknown
plain_text
2 years ago
4.6 kB
8
Indexable
#include "cipher.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h> // для работы с директориями
// Функция безопасного считывания строки
void safeScanf(char* str, int size) {
fgets(str, size, stdin);
str[strcspn(str, "\n")] = 0;
}
// Функция чтения файла и вывода его содержимого
void readFile(const char *filepath) {
FILE *file = fopen(filepath, "r");
if (file == NULL) {
printf("n/a\n");
return;
}
int character = fgetc(file);
if (character == EOF) {
printf("n/a\n");
} else {
do {
putchar(character);
character = fgetc(file);
} while (character != EOF);
}
fclose(file);
printf("\n");
}
// Функция добавления текста в файл
void appendToFile(const char *filePath) {
char text[256];
fgets(text, sizeof(text), stdin);
text[strcspn(text, "\n")] = 0;
FILE *file = fopen(filePath, "r");
if (file != NULL) {
fclose(file);
file = fopen(filePath, "a");
fprintf(file, "%s\n", text);
fclose(file);
readFile(filePath);
} else {
printf("n/a\n");
}
}
// Функция шифрования кодом Цезаря
void caesar_cipher(char *input, int shift) {
for (int i = 0; input[i] != '\0'; i++) {
char ch = input[i];
if (ch >= 'a' && ch <= 'z') {
ch = (ch - 'a' + shift) % 26 + 'a';
} else if (ch >= 'A' && ch <= 'Z') {
ch = (ch - 'A' + shift) % 26 + 'A';
}
input[i] = ch;
}
}
// Функция обработки директории
void processDirectory(const char *dirPath, int shift) {
DIR *d;
struct dirent *dir;
d = opendir(dirPath);
if (d) {
while ((dir = readdir(d)) != NULL) {
// Обрабатываем только .c и .h файлы
if (strstr(dir->d_name, ".c") || strstr(dir->d_name, ".h")) {
char fullPath[512];
sprintf(fullPath, "%s/%s", dirPath, dir->d_name);
if (strstr(dir->d_name, ".c")) {
// Шифровать содержимое .c файла
FILE *file = fopen(fullPath, "r+");
if (file) {
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
char *content = (char *)malloc(fileSize + 1);
fread(content, 1, fileSize, file);
content[fileSize] = '\0';
caesar_cipher(content, shift);
fseek(file, 0, SEEK_SET);
fwrite(content, 1, strlen(content), file);
truncate(fullPath, strlen(content)); // Обрезать файл до новой длины
fclose(file);
free(content);
}
} else if (strstr(dir->d_name, ".h")) {
// Очистить содержимое .h файла
FILE *file = fopen(fullPath, "w");
fclose(file);
}
}
}
closedir(d);
}
}
int main() {
char dirPath[256] = "";
int shift = 0;
while (1) {
int choice;
printf("Choose an option:\n1. Read file\n2. Append to file\n3. Process directory\n");
if (scanf("%d", &choice) != 1) {
printf("n/a\n");
return 0;
}
getchar(); // Чтобы очистить буфер ввода после scanf
if (choice == -1) {
break;
}
switch (choice) {
case 1:
safeScanf(dirPath, sizeof(dirPath));
readFile(dirPath);
break;
case 2:
if (strlen(dirPath) == 0) {
printf("n/a\n");
} else {
appendToFile(dirPath);
}
break;
case 3:
printf("Enter directory path: ");
safeScanf(dirPath, sizeof(dirPath));
printf("Enter Caesar cipher shift amount: ");
scanf("%d", &shift);
processDirectory(dirPath, shift);
break;
default:
printf("n/a\n");
break;
}
}
return 0;
}
Editor is loading...
Leave a Comment