Mega Code Archive

 
Categories / C# / File Stream
 

Demonstrates attaching a StreamReader object to a stream

/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // StrmRdr.cs -- Demonstrates attaching a StreamReader object to a stream // //               Compile this program with the following command line: //                   C:>csc StrmRdr.cs using System; using System.IO; namespace nsStreams {     public class StrmRdr     {         static public void Main (string [] args)         {             if (args.Length == 0)             {                 Console.WriteLine ("Please enter a file name");                 return;             }             FileStream strm;             StreamReader reader;             try             {                 // Create the stream                 strm = new FileStream (args[0], FileMode.Open, FileAccess.Read);                 // Link a stream reader to the stream                 reader = new StreamReader (strm);             }             catch (Exception e)             {                 Console.WriteLine (e.Message);                 Console.WriteLine ("Cannot open " + args[0]);                 return;             }             while (reader.Peek() >= 0)             {                 string text = reader.ReadLine ();                 Console.WriteLine (text);             }             reader.Close ();             strm.Close ();         }     } }