Character Streams — A Brief Summary

 

The mathematical model:

The mathematical model for both Character_IStream and Character_OStream is

 

math subtype CHARACTER_STREAM_MODEL is (

is_open: boolean,

ext_name: string of character,

content: string of character

)

exemplar cs

constraint

if cs.is_open = false

then (cs.ext_name = empty_string and

cs.content = empty_string)

 

·        is_open keeps track of whether a Character_IStream has been connected (opened) to an input source or whether a Character_OStream has been connected (opened) to an output destination.

·        ext_name keeps track of the name of the input source for Character_IStream or the name of the output destination for Character_OStream.

·        content keeps track of the string of input characters for Character_IStream or of the string of output characters for Character_OStream.

For both Character_IStream and Character_OStream, initialization ensures that is_open = false.

 

Opening and closing streams:

Assume that input is an object of type Character_IStream, that output is an object of type Character_Ostream, and that file_name is an object of type Text.

 

·        To open input to the keyboard use input.Open_External ("")

·        To open input to a file use input.Open_External ("some file name") or input.Open_External (file_name).

·        To close input use input.Close_External ().

 

·        To open output to the screen use output.Open_External ("")

·        To open output to a file use output.Open_External ("some file name") or output.Open_External (file_name).

·        To close output use output.Close_External ().

 

Input:

Input operations using Character_IStream always read an entire line of input.  For example, suppose that input is an object of type Character_IStream and that j is an object of type Integer.  Also, suppose that input.content = "  1234\t\t \t\t\t \t\n9876 . . . ".  Then, after the input operation input >> j, the value of j will be 1234 and input.content = "9876 . . . ".  Notice that the entire string of characters "  1234\t\t \t\t\t \t\n" was removed from input.content, even though only the characters "1234" were used to get a value for j.  The new line character '\n' marks the start of a new line, and thus the end of the previous line as well.