Commit b66df99a by Rudolf

Use standard FILE I/O as parameters

While stdin/stdout is all nice and dandy for piping, we want to show useful
information about compression, so remove this feature.
parent 619a665a
Showing with 39 additions and 3 deletions
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
...@@ -83,10 +84,45 @@ int decode_file(FILE *in, FILE *out) ...@@ -83,10 +84,45 @@ int decode_file(FILE *in, FILE *out)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if (argc == 2 && !strcmp(argv[1], "-d")) { const char *infile, *outfile;
decode_file(stdin, stdout); bool decompress = false;
if (argc > 1)
decompress = !strcmp(argv[1], "-d");
if ((decompress && argc < 4) || (!decompress && argc != 3)) {
printf("Usage: huffman [-d] <input file> <output file>\n");
return -1;
}
/* Gotta get that decompression going on. */
if (decompress) {
infile = argv[2];
outfile = argv[3];
} else { } else {
encode_file(stdin, stdout); infile = argv[1];
outfile = argv[2];
}
FILE *input = fopen(infile, "r");
if (!input) {
fprintf(stderr, "Failed to open input file\n");
return -1;
}
FILE *output = fopen(outfile, "w");
if (!output) {
fprintf(stderr, "Failed to open output file\n");
fclose(input);
return -1;
} }
if (decompress)
decode_file(input, output);
else
encode_file(input, output);
fclose(input);
fclose(output);
return 0; return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment