Exercise index of this lecture   Alphabetic index   Course home   

Input and Output Classes

10.1   A variant of the file copy program  

The purpose of this exercise is to train the use of the Read method in class Stream, and subclasses of class Stream.

Write a variant of the file copy program. Your program should copy the entire file into a byte array. Instead of the method ReadByte you should use the Read method, which reads a number of bytes into a byte array. (Please take a careful look at the documentation of Read in class FileStream before you proceed). After this, write out the byte array to standard output such that you can assure yourself that the file is read correctly.

Are you able to read the entire file with a single call to Read? Or do you prefer to read chunks of a certain (maximum) size?



10.2   Finding the encoding of a given text file  

Make a UTF-8 text file with some words in Danish. Be sure to use plenty of special Danish characters. You may consider to write a simple C# program to create the file. You may also create the text file in another way.

In this exercise you should avoid writing a byte order mark (BOM) in your UTF-8 text file. (A BOM in the UTF-8 text file may short circuit the decoding we are asking for later in the exercise). One way to avoid the BOM is to denote the UTF-8 encoding with new UTF8Encoding(), or equivalently new UTF8Encoding(false). You may want to consult the constructors in class UFT8Encoding for more information.

Now write a C# program which systematically - in a loop - reads the text file six times with the following objects of type Encoding: ISO-8859-1, UTF-7, UTF-8, UTF-16 (Unicode), UTF32, and 7 bits ASCII.

More concretely, I suggest you make a list of six encoding objects. For each encoding, open a TextReader and read the entire file (with ReadToEnd, for instance) with the current encoding. Echo the characters, which you read, to standard output.

You should be able to recognize the correct, matching encoding (UTF-8) when you see it.



10.3   Die tossing - writing to text file  

Write a program that tosses a Die 1000 times, and writes the outcome of the tosses to a textfile. Use a TextWriter to accomplish the task.

Write another program that reads the text file. Report the number of ones, twos, threes, fours, fives, and sixes.



10.4   Die tossing - writing to a binary file  

This exercise is a variant of the die tossing and file writing exercise based on text files.

Modify the program to use a BinaryWriter and a BinaryReader.

Take notice of the different sizes of the text file from the previous exercise and the binary file from this exercise. Explain your observations.



10.5   Serializing one of your own classes  

The purpose of this exercise is to let you have your first experiences with serialization of some of your own object, which are instances of your own classes.

Select one or more of your own classes, typically from the project on which you are working.

Mark the classes as Serializable, in the same way as we have done in class Person and class Date on the accompanying slide page.

Consider if some of the instance variables should be marked as NonSerialized and if an OnDeserialized method should be provided.

Like in the client program of class Person and class Date, make a sample client class of your own classes, and call the Serialize and the Deserialize methods.


10.6   Serializing with an XML formatter  

In the programs shown on the accompanying slide we have used a binary formatter for serialization of Person and Date object.

Modify the client program to use a so-called Soap formatter in the namespace System.Runtime.Serialization.Formatters.Soap. SOAP is an XML language intended for exchange of XML documents. SOAP is related to the discipline of web services in the area of Internet technology.

After the serialization you should take a look at the file person.dat, which is written and read by the client program.



Generated: Monday February 7, 2011, 12:19:56