-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar.c
More file actions
78 lines (66 loc) · 2.08 KB
/
caesar.c
File metadata and controls
78 lines (66 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
void caesarCypher(char* filePath, char* key, bool decypher) {
// Determine operation based on decipher flag
int (*operation)(int, int); // Function pointer
operation = decypher ? subtract : add; // Works as an operator overload
if (filePath != NULL) {
FILE* file = fopen(filePath, "rb+");
if (!file) {
perror("fopen");
exit(EXIT_FAILURE);
}
fseek(file, 0, SEEK_END); // Aller à la fin du fichier
long taille = ftell(file);
fseek(file, 0, SEEK_SET); // Retourner au début
// Read and print each character from the file
int c;
char *tab1 = malloc((size_t)taille);
char *tab2 = malloc((size_t)taille);
if (tab1 == NULL || tab2 == NULL) {
perror("malloc");
fclose(file);
exit(EXIT_FAILURE);
}
size_t idx = 0;
while ((c = fgetc(file)) != EOF && idx < (size_t)taille) {
// putchar(c);
tab1[idx] = (char)c;
// increment character for tab2
tab2[idx] = (char)(operation(c, (int) *key));
idx++;
}
printf("Before : ");
for (int j = 0; j < idx; j++) {
printf("0x%02x ", (unsigned char)tab1[j]);
}
printf("\r\n");
for (int j = 0; j < idx; j++) {
putchar(tab1[j]);
}
printf("\n\rAfter : ");
for (int j = 0; j < idx; j++) {
printf("0x%02x ", (unsigned char)tab2[j]);
}
printf("\r\n");
for (int j = 0; j < idx; j++) {
putchar(tab2[j]);
}
printf("\r\n");
// Write modified content back to the file
fseek(file, 0, SEEK_SET); // Retourner au début
fwrite(tab2, sizeof(char), (size_t)taille, file);
// Close the file after reading & free memory
free(tab1);
free(tab2);
fclose(file);
}
}