Guest User


a guest
Apr 24th, 2019
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 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.
  2. 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:
  3. int pack( char a, char b, char c, char d )
  4. {
  5. int p = a;
  6. p = (p << CHAR_BIT) | b;
  7. p = (p << CHAR_BIT) | c;
  8. p = (p << CHAR_BIT) | d;
  9. return p;
  10. 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.
  11. 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:
  12. char unpack( int p, int k )
  13. {
  14. unsigned mask = 0xFF;
  15. int n = k * CHAR_BIT;
  16. mask <<= n;
  17. return ( ( p & mask ) >> n );
  18. }
  19. Then, your Makefile will have to compile the corresponding "c" file you generated.
  20. 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");
  21. You should have corresponding header files for compilation and linking.
  22. Please follow all steps and explain along the way.
  23. Thanks!
RAW Paste Data