a guest Apr 24th, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- Given an arbitrary text file, your program will pack the ascii code characters into integers. Specfically, 4 characters will be encoded into one unsigned integer. Then, you save your integers into a binary file. Then, the program will read the binary file, and decode the contents into the original text, and save the text in a text file. Here are some basic components for your program design. To simplify the matter, we'll use HW9.txt as input file, HW9.bin as binary output, and HW9c.txt as text output.
- In "encoding.c", you have a function "encoding" which opens a text fiile "HW9.txt" for reading and a binary file "HW9.bin" for wrting. Then, read 4 characters at a time, pack them into an integer by a function "pack", manipulate the integer by "encryption", and write the integer into "HW9.bin" until the end of "HW9.txt". Using something like this:
- int pack( char a, char b, char c, char d )
- int p = a;
- p = (p << CHAR_BIT) | b;
- p = (p << CHAR_BIT) | c;
- p = (p << CHAR_BIT) | d;
- return p;
- At the end of file, you may save some NULL ('') characters to make it 4 characters for the integer, so you know NULL means the end of the input. Close all the files. The encryption part is optional.
- In "decoding.c", you have a function "decoding" which opens "HW9.bin" for reading and a text file "HW9c.txt" for writing. Then, read an integer at a time, use the key from your partner to manipulate the integer by a function "decryption", unpack it into 4 characters by a function called "unpack", and save the 4 characters into "HW9c.txt". Close all the files. If there is no encryption, there is no decryption. Using something like this:
- char unpack( int p, int k )
- unsigned mask = 0xFF;
- int n = k * CHAR_BIT;
- mask <<= n;
- return ( ( p & mask ) >> n );
- Then, your Makefile will have to compile the corresponding "c" file you generated.
- The "main.c" is simple: display the content of "HW9.txt", call encoding, call decoding, and display the contents of "HW9c.txt". You can display the content of a file by a system call as follows, for example: system ("cat HW9.txt");
- You should have corresponding header files for compilation and linking.
- Please follow all steps and explain along the way.
RAW Paste Data