Hi
I am trying to make a very easy converter/compressor; the program should take a file with 4 different types of ASCII characters and writ it out as binary to a file. The program should also read the binary file and convert it to ASCII and print it out on the screen. Under is my code, I can’t really get the char/cstring. What types of improvement must I do to get this to work?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char compresser(char c);
char converter(char c);
int main(int argc, char **argv)
{
char *c;
FILE *If = fopen("A.txt", "r");
FILE *Uf = fopen("B.txt", "rw");
if(If == NULL || Uf == NULL) {
printf("Could not open file");
}
if(argc < 4) {
printf("Too few argument, must be 3\n");
} else if(strcmp(argv[1], "p") == 0) {
while((c = fgetc(If)) != EOF) {
printf("%c", c);
}
} else if(strcmp(argv[1], "e") == 0) {
while((c = fgetc(If)) != EOF) {
fprintf(Uf, "%c\n", compresser(c));
}
} else if(strcmp(argv[1], "d") == 0) {
while((c = fgetc(Uf)) != EOF) {
printf("%c", converter(c));
}
} else {
printf("Not a valid command\n");
}
}
char compresser(char c)
{
if(c == ' ') {
return '00';
} else if(c == ':') {
return '01';
} else if(c == '@') {
return '10';
} else if(c == '\n') {
return '11';
} else {
return 'e';
}
}
char converter(char c)
{
if(c == '00') {
return ' ';
} else if(c == '01') {
return ':';
} else if(c == '10') {
return '@';
} else if(c == '11') {
return '\n';
} else {
return 'e';
}
}