1 Huffman Codes Using Binary Files. 2 Getting Started Last class we extended a program to create a Huffman code and permit the user to encode and decode.

Slides:



Advertisements
Similar presentations
CMSC 2021 C++ I/O and Other Topics. CMSC 2022 Using C++ Stream I/O Default input stream is called cin Default output stream is called cout Use the extraction.
Advertisements

File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
© 2000 Scott S Albert Structured Programming 256 Chapter 7 Streams and File I/O.
1 Text File I/O Chapter 6 Pages File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.
STARTING OUT WITH STARTING OUT WITH Class 15 Honors.
Chapter 5: Loops and Files.
File I/O Supplemental Material. Background In C++, files can be manipulated in the same manner we manipulate streams such as: cout and cin. Therefore,
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
11 Introduction to Object Oriented Programming (Continued) Cats II.
Binary Search Trees II Morse Code.
CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a.
Darbas su failais Arnas Terekas IT 1gr. Vilniaus universitetas Matematikos ir informatikos fakultetas.
1 Huffman Codes Drozdek Chapter Objectives You will be able to Construct an optimal variable bit length code for an alphabet with known probability.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
File I/O ifstreams and ofstreams Sections 11.1 &
Chapter 9 I/O Streams and Data Files
Topics 1.File Basics 2.Output Formatting 3.Passing File Stream Objects to Functions 4.More Detailed Error Testing 5.Member Functions for Reading and 6.Writing.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
1 Command Processor V Saving Changes. Command Processor We need to be able to save changes to the database that result from user actions. Download example.
File I/O in C++ II. Open() function Open() is a member function in each classes ( fstream, ifstream, ofstream) Void fstream :: open ( const char *filename,
FILE I/O IN C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
1 Command Processor II. 2 Command Processor Example Let's look at a simple example of a command processor using states and cities. Get initial information.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes.
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Huffman Codes Drozdek Chapter Encoding Next we will add the capability to encode a message entered as normal text. The Huffman Tree that we use.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes. Understand the meaning of polymorphism and how it works.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Lecture 14 Arguments, Classes and Files. Arguments.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
Writing to Files and Reading From Files. Writing Data to a File Creating a new file, a.dat or.txt file #include #include // for file writing #include.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
1 Huffman Codes Computing an Optimal Code for a Document.
1 Implementing Ticket Printer. Download project from last class Downloads/2016_02_12_In_Class/
1 Command Processor. Objectives You will be able to Create and understand State Diagrams. Write a text based command processor with multilevel menus.
1 Using an XML Parser. 2 Objective You will be able to use a publically available open source parser to convert an XML file into an internal data structure.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
C++ Lesson 1.
Chapter 1.2 Introduction to C++ Programming
ifstreams and ofstreams
Chapter 1.2 Introduction to C++ Programming
MT262A Review.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
File I/O.
Today’s Lecture I/O Streams Tools for File I/O
Standard Input/Output Stream
No I/O is built into C++ instead, a library provides input stream and output stream Keyboard Screen executing program istream ostream 1.
CPS120: Introduction to Computer Science
CHAPTER 4 File Processing.
Using string type variables
ifstreams and ofstreams
File I/O in C++ II.
File I/O in C++ I.
Presentation transcript:

1 Huffman Codes Using Binary Files

2 Getting Started Last class we extended a program to create a Huffman code and permit the user to encode and decode messages. We will use that program as our starting point today: _04_13_Huffman_Codes_with_Binary_IO/ _04_13_Huffman_Codes_with_Binary_IO/ File Huffman_Code_with_Associative_Map.zip Download, extract, built, and run.

3 Program in Action Widen window to 100.

4 Binary Output Huffman codes are useful in real life only when we output the coded message as binary. Let's modify do_encode to output a file. Start with a text file. ASCII 1's and 0's Modify to write a binary file.

5 main.cpp Add at top of main.cpp: #include Modified version of do_encode: _04_13_Huffman_Codes_with_Binary_IO/do_encode.cpp.txt _04_13_Huffman_Codes_with_Binary_IO/do_encode.cpp.txt

6 Modifications to do_encode() void do_encode(void) { string msg; string output_filename; ofstream outfile; string junk; while (!outfile.good()) { cout << "File name for output? "; cin >> output_filename; getline(cin,junk); // Skip newline char outfile.open(output_filename.c_str()); if (!outfile.good()) { cout << "Failed to open output file\n"; cout << "Please try again\n"; }

7 Modifications to do_encode() cout << "\n\nEnter message to encode\n"; getline(cin, msg); for (size_t i = 0; i < msg.length(); ++i) { char next_char = tolower(msg[i]); string code = huffman_tree.Encode_Char(next_char); cout << code; outfile << code; } cout << endl << endl; outfile << endl << endl; outfile.close(); cout << "File " << output_filename << " written\n"; }

8 Clean Up Output Comment out statements that output the tree and the code. In main(): int main(void) { cout << "This is the Huffman Code Program" << endl; build_huffman_tree(); //huffman_tree.Display_List();

9 In Huffman_Tree.cpp void Huffman_Tree::Make_Decode_Tree(void) { node_list.sort(); //cout << "\nSorted list:\n"; //Display_List();... //cout << endl << "The Huffman Tree" << endl; //Display_Decode_Tree(&decode_tree_root, 0); //cout << endl << "The Code: " << endl; //Display_Code(&decode_tree_root, ""); }

10 Program in Action Examine c:\out.txt

The Output File 11

12 Invalid Characters What should we do with characters that are not in the code? Encode_Char() returns a zero length string. Detect the error in do_encode(). Tell user about the error. Skip the invalid character in output.

13 main.cpp In do_encode() for (size_t i = 0; i < msg.length(); ++i) { char next_char = tolower(msg[i]); string code = huffman_tree.Encode_Char(next_char); if (code.size() == 0) { cout << endl << "Invalid character in input to do_encode: " << next_char << endl; continue; } cout << code; outfile << code << " "; }

14 Program Running

15 The Output File

16 Binary File I/O Issues with binary files. Hardware architecture dependencies. Code is typically not portable. Output is by byte, not by bit For Huffman coding we need variable length bit strings. Must know number of bits. Encapsulate code to do binary file I/O in classes. Provide relatively simple interface to the rest of the program.

17 Binary Output File Class Bit Count Bits Client Classes Binary Output File Class Buffer

18 Binary Input File Class Bit Count Bits Client Classes Binary Input File Class Buffer

19 Binary File Classes Binary_File is_open buffer next_bit_position filename BUFFER_SIZE FIRST_BIT_POSITION + Is_Open Binary_Output_File -fstream + Output_Bit_String + Close - Write_Buffer Binary_Input_File -fstream + Get_Next_Bit + Close -Read_Buffer

20 Binary File I/O Download _04_13_Binary_File_IO/ _04_13_Binary_File_IO/ File Binary_File_IO_Classes.zip

21 Binary File IO Classes Copy into project folder and add to project.

22 Add Binary File IO Files to Project Build project.

23 Binary_File.h #pragma once #include using std::string; class Binary_File { public: Binary_File(const string& Filename); virtual void Close() = 0; bool Is_Open() const {return is_open;}; protected: static const int BUFFER_SIZE = 1024; // Size in bytes static const int FIRST_BIT_POSITION = 8*sizeof(size_t); union Buffer { char bits[BUFFER_SIZE]; size_t bit_count; }; void Reset_Buffer(void); const string filename; bool is_open; Buffer buffer; size_t next_bit_position; };

24 Binary_File.cpp #include "Binary_File.h" Binary_File::Binary_File(const string& Filename) : is_open(false), filename(Filename) { Reset_Buffer(); } void Binary_File::Reset_Buffer(void) { for (int i = 0; i < BUFFER_SIZE; ++i) { buffer.bits[i] = 0; } next_bit_position = FIRST_BIT_POSITION; }

25 Binary_Output_File.h #pragma once #include #include "Binary_File.h" using std::string; class Binary_Output_File : public Binary_File { public: Binary_Output_File(const string& filename); void Output(const string& bit_string); void Close(); private: std::fstream outfile; void Write_Buffer(); };

26 Binary_Output_File.cpp #include #include "Binary_Output_File.h" using namespace std; Binary_Output_File::Binary_Output_File(const string& filename) : Binary_File(filename) { outfile.open(filename.c_str(), ios::out | ios::binary ); if (outfile.fail()) { string err_msg("Error opening output file "); err_msg += filename; throw err_msg; } Reset_Buffer(); is_open = true; }

27 Binary_Output_File.cpp void Binary_Output_File::Write_Buffer() { assert (is_open); if (next_bit_position == FIRST_BIT_POSITION) { return; } buffer.bit_count = next_bit_position - FIRST_BIT_POSITION; size_t nr_bytes = (size_t) ceil(next_bit_position / 8.0); outfile.write( buffer.bits, nr_bytes); Reset_Buffer(); }

Binary_Output_File.cpp void Binary_Output_File::Output(const string& bit_string) { assert(is_open); for (size_t i = 0; i < bit_string.size(); ++i) { if (bit_string[i] == '1') { size_t byte_position = next_bit_position / 8; size_t bit_position_within_byte = next_bit_position % 8; buffer.bits[byte_position] |= (0x80 >> bit_position_within_byte); } else { assert(bit_string[i] == '0'); } ++next_bit_position; if (next_bit_position == BUFFER_SIZE*8) { Write_Buffer(); }

29 Binary_Output_File.cpp void Binary_Output_File::Close() { Write_Buffer(); outfile.close(); is_open = false; }

30 Using Binary File IO Now let's modify do_encode() to write a binary file. Add at top of main.cpp: #include "Binary_Output_File.h"

31 do_encode() void do_encode(void) { string msg; string output_filename; Binary_Output_File* outfile; string junk; while (true) { cout << "File name for output? "; cin >> output_filename; getline(cin, junk); // Skip newline char try { outfile = new Binary_Output_File(output_filename); break; } catch (const string& msg) { cout << msg << endl; }

32 do_encode() cout << "Enter message to encode\n"; getline(cin, msg); for (size_t i = 0; i < msg.length(); ++i) { char next_char = tolower(msg[i]); string code = huffman_tree.Encode_Char(next_char); if (code.size() == 0) { cout << endl << "Invalid character in input to do_encode: " << next_char << endl; continue; } cout << code; outfile->Output(code); }

33 do_encode() cout << endl << endl; outfile->Close(); delete(outfile); cout << "File " << output_filename << " written\n"; }

34 Some Test Data

35 Program in Action

36 c:\test.dat Look at the output file in Visual Studio File > Open > File Bit Count =

37 Binary Input Now let's modify do_decode() to read a binary input file rather than reading 1's and 0's from the keyboard. Add at top of main.cpp: #include "Binary_Input_File.h" _04_13_Huffman_Codes_with_Binary_IO/do_decode.cpp.t xt

38 do_decode() void do_decode(void) { string msg; string input_filename; Binary_Input_File* infile; string junk; while (true) { cout << "File name for input? "; cin >> input_filename; getline(cin, junk); // Skip newline char try { infile = new Binary_Input_File(input_filename); break; } catch (const string& msg) { cout << msg << endl; }

39 do_decode() string coded_message = ""; string original_message; while (infile->Is_Open()) { int next_bit = infile->Get_Next_Bit(); if (next_bit < 0) break; if (next_bit == 0) { coded_message += "0"; } else { coded_message += "1"; } original_message = huffman_tree.Decode_Msg(coded_message); cout << "Original message: " << original_message << endl; cout << endl << endl; }

40 Reading a Binary File

41 Another Example