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? |
Solution
Here is a solution:
In the solution above we allocate a byte array, which is large enough to hold the entire fromFile. In the do-while loop we repeatedly read chunks of chunkSize (100) bytes. After outputting the bytes in strmContent to standard output we report the number of calls to Read. |
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. |
Solution
The following program generates a UTF-8 encoded text file:
The program writes the Danish string to the file guess-me.txt Here follows the program that reads guess-me.txt with six different encodings:
Notice that the program expects you to pass the name of the text file, guess-me.txt, as a program parameter. The output is something like:
We confirm from this that the original file is UTF8 encoded. |
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. |
Solution
The output part of the program is here:
Take at look at the file written by your program. Here is an outline of the output written by my version of the program:
The reading part of the program is here:
Based on the input read by the program, it generates the following on standard output - the screen:
|
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. |
Solution
There are only a few differences between this solution and the corresponding text file solution. The output part of the program is here:
The size of the binary file is 4000 bytes, namely four bytes per integer. (This is actually waste of space. Can you do it better?) The size of the text file generated by the program from the previous exercise is 3000 bytes, namely one byte representing either '1', '2', '3', '4', '5', or '6' and two bytes for newline and carriage return (Windows end-of-line convention). Notice that we - in general - expect smaller binary files than the similar text files! The reading part of the program is here:
|
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. |
Solution
Here follows the program that serializes and deserializes the person and date objects to an XML file:
The XML file generated by the serialization is here:
|
Generated: Monday February 7, 2011, 12:19:56