-
Notifications
You must be signed in to change notification settings - Fork 0
/
single-thread-zip.c
42 lines (37 loc) · 975 Bytes
/
single-thread-zip.c
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
#include <stdio.h>
#include <stdlib.h>
void compressFile(FILE *fp) {
int count;
char current, previous;
if ((previous = fgetc(fp)) != EOF) {
count = 1;
while ((current = fgetc(fp)) != EOF) {
if (current == previous) {
count++;
} else {
fwrite(&count, sizeof(int), 1, stdout);
fwrite(&previous, sizeof(char), 1, stdout);
previous = current;
count = 1;
}
}
fwrite(&count, sizeof(int), 1, stdout);
fwrite(&previous, sizeof(char), 1, stdout);
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("wzip: file1 [file2 ...]\n");
return 1;
}
for (int i = 1; i < argc; i++) {
FILE *fp = fopen(argv[i], "r");
if (fp == NULL) {
perror("wzip");
return 1;
}
compressFile(fp);
fclose(fp);
}
return 0;
}