[Courses] [C] lesson13: The last two exercises, done :)
Morgon Kanter
admin at surgo.net
Sat Dec 7 02:43:12 EST 2002
First, on the ascii-to-binary and the binary-to-ascii.
/******* BEGIN ASCII-TO-BINARY ********/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, char *argv[]) {
FILE *input_file, *output_file;
char input[80];
long long in;
if(argc != 3) {
fprintf(stderr, "Error: Incorrect number of arguments\n");
fprintf(stderr, "Usage: asciibin inputfile outputfile\n");
exit(8);
}
input_file = fopen(argv[1], "r");
if(errno == ENOENT) {
fprintf(stderr, "Error: Unable to open %s\n", argv[1]);
exit(8);
}
output_file = fopen(argv[2], "w");
if(output_file == NULL) {
fprintf(stderr, "Error: Unable to open %s for writing\n", argv[2]);
exit(8);
}
fgets(input, sizeof(input), input_file);
in = atoll(input);
fwrite(&in, 1, sizeof(in), output_file);
fclose(input_file);
input_file = NULL;
fclose(output_file);
output_file = NULL;
puts("Done");
return 0;
}
/****** END ASCII-TO-BINARY *******/
Okay, it read from an input-file: 33
It outputted to a file: !^@^@^@^@^@^@^@ (approximation, kedit couldn't handle
that ;)
Now to translate it back:
/******* BEGIN BINARY-TO-ASCII *******/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, char *argv[]) {
FILE *input_file, *output_file;
long long input;
char output[10];
if(argc != 3) {
fprintf(stderr, "Error: improper number of arguments\n");
fprintf(stderr, "Usage: binascii inputfile outputfile\n");
exit(8);
}
/* Open files and check for errors */
input_file = fopen(argv[1], "r");
if(errno == ENOENT) {
fprintf(stderr, "Error: Unable to open %s for reading\n", argv[1]);
exit(8);
}
output_file = fopen(argv[2], "w");
if(output_file == NULL) {
fprintf(stderr, "Error: Unable to open %s for writing\n", argv[2]);
exit(8);
}
fread(&input, 1, sizeof(input), input_file);
sprintf(output, "%lli", input);
fputs(output, output_file);
puts("Done");
return 0;
}
/******* END BINARY-TO-ASCII ********/
The input: the same as last one's output
The output: 33, the same as the first one's input. See, it works :)
-----------------------------------------------------------------------
And the last exercise, write a program to copy one file to another file but
remove the high bit (0x80). First, I wrote a small program to write the "high
bit" to a file along with a couple of other things. Now here is the
un-highbit program:
/******* BEGIN UN-HIGHBIT *******/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, char *argv[]) {
FILE *input_file, *output_file;
char current_char;
if(argc != 3) {
fprintf(stderr, "Error: incorrect number of arguments\n");
fprintf(stderr, "Usage: unhighbit infile outfile\n");
exit(8);
}
input_file = fopen(argv[1], "r");
if(errno == ENOENT) {
fprintf(stderr, "Error: couldn't open %s for reading\n", argv[1]);
exit(8);
}
output_file = fopen(argv[2], "w");
if(output_file == NULL) {
fprintf(stderr, "Error: couldn't open %s for writing\n", argv[2]);
exit(8);
}
current_char = fgetc(input_file);
while(current_char != EOF) {
if((current_char & 0x80) != 0) goto end_of_loop;
fputc(current_char, output_file);
end_of_loop:
current_char = fgetc(input_file);
}
fclose(input_file);
input_file = NULL;
fclose(output_file);
output_file = NULL;
puts("Done");
return 0;
}
/********* END UN-HIGHBIT *********/
The input file read the following:
Yo[highbit]Cya
Output file then read:
YoCya
End of lesson13 :)
Morgon
--
grab my updated (11/25/02) public key from http://www.surgo.net/pubkey.asc
More information about the Courses
mailing list