Home   Cover Cover Cover Cover
 

Hauptspeicherströme

A12.cs
using System;
using System.IO;

// Writes a multiplication table to a MemoryStream and
// reads it back computing a check sum
public class Streams {
  
  static void Main(string[] arg) {
    MemoryStream s = new MemoryStream();
    //--- output to the MemoryStream
    StreamWriter w = new StreamWriter(s);
    for (int i = 1; i < 10; i++) {
      for (int j = 1; j < 10; j++)
        w.Write("{0,4}", i*j);
      w.WriteLine();
    }
    w.Flush();
    //--- input from the MemoryStream
    s.Seek(0, SeekOrigin.Begin);
    StreamReader r = new StreamReader(s);
    int check = 0;
    int ch = r.Read();
    while (ch >= 0) {
      Console.Write((char)ch);
      check ^= ch;
      ch = r.Read();
    }
    Console.WriteLine("\ncheck sum = {0}", check);
    r.Close();
    s.Close();
  }
}