-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata compresser tool.cpp
More file actions
91 lines (72 loc) · 2.18 KB
/
Copy pathdata compresser tool.cpp
File metadata and controls
91 lines (72 loc) · 2.18 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
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function to perform Run Length Encoding (RLE)
void compressRLE(const char *input, const char *output) {
FILE *inFile = fopen(input, "r");
FILE *outFile = fopen(output, "w");
if (inFile == NULL || outFile == NULL) {
perror("Error opening file");
exit(EXIT_FAILURE);
}
char current, prev;
int count = 1;
prev = fgetc(inFile);
while ((current = fgetc(inFile)) != EOF) {
if (current == prev) {
count++;
} else {
fprintf(outFile, "%c%d", prev, count);
prev = current;
count = 1;
}
}
// Write the last character and its count
if (prev != EOF) {
fprintf(outFile, "%c%d", prev, count);
}
fclose(inFile);
fclose(outFile);
printf("Compression completed. Output saved to %s\n", output);
}
// Function to decompress RLE encoded data
void decompressRLE(const char *input, const char *output) {
FILE *inFile = fopen(input, "r");
FILE *outFile = fopen(output, "w");
if (inFile == NULL || outFile == NULL) {
perror("Error opening file");
exit(EXIT_FAILURE);
}
char ch;
int count;
while (fscanf(inFile, "%c%d", &ch, &count) == 2) {
for (int i = 0; i < count; i++) {
fputc(ch, outFile);
}
}
fclose(inFile);
fclose(outFile);
printf("Decompression completed. Output saved to %s\n", output);
}
int main() {
char inputFile[100], outputFile[100];
int choice;
printf("Run Length Encoding (RLE) Program\n");
printf("1. Compress File\n");
printf("2. Decompress File\n");
printf("Enter your choice: ");
scanf("%d", &choice);
printf("Enter input file name: ");
scanf("%s", inputFile);
printf("Enter output file name: ");
scanf("%s", outputFile);
if (choice == 1) {
compressRLE(inputFile, outputFile);
} else if (choice == 2) {
decompressRLE(inputFile, outputFile);
} else {
printf("Invalid choice. Exiting.\n");
exit(EXIT_FAILURE);
}
return 0;
}