< AuthenticatedStream, StreamWriter & StreamReader Class Examples | Main | BinaryReader & BinaryWriter Examples >

 


 

Chapter 2 Part 15:

Managed I/O - Streams, Readers, and Writers

 

 

What do we have in this chapter 2 Part 15?

  1. C++ StreamReader and StreamWriter Program Example

  2. C# StreamReader and StreamWriter Program Example

  3. VB .NET StreamReader and StreamWriter Program Example

 

 

 

Note: If you want to experience a complete C++ .NET/C++-CLI programming tutorial please jump to Visual C++ .NET programming tutorial.

 

C++ StreamReader and StreamWriter Example

 

Create a new CLR console application project and you might want to use SimpleWriterReaderCP as the project and solution names.

 

C++ StreamReader and StreamWriter Example - a new CLR console mode application project creation

 

Add the following code.

 

// SimpleWriterReaderCP.cpp : main project file.

 

#include "stdafx.h"

 

using namespace System;

using namespace System::IO;

 

int main(array<System::String ^> ^args)

{

        String^ path = L"c:\\temp\\MyTestFile0.txt";

 

        try

        {

             if (File::Exists(path))

            {

                   File::Delete(path);

            }

 

           Console::WriteLine(L"Writing some text to {0}", path);

           StreamWriter^ sw = gcnew StreamWriter(path);

          sw->WriteLine("This");

          sw->WriteLine("is some text");

          sw->WriteLine("written to test");

          sw->WriteLine("for reading...");

          sw->Close();

 

          Console::WriteLine("Reading the previously written text..");

          FileStream^ fs = gcnew FileStream(path, FileMode::Open);

          StreamReader^ sr = gcnew StreamReader(fs);

         

           while (sr->Peek() >= 0)

           {

                  Console::WriteLine(sr->ReadLine());

            }

        }

        catch (Exception^ e)

        {

               Console::WriteLine("The process failed: {0}", e->ToString());

        }

    return 0;

}

 

Build and run the project. The following is an output example.

 

C++ StreamReader and StreamWriter Example - a sample of console mode output

 

You can try opening the created text file and in this case is shown below.

 

C++ StreamReader and StreamWriter Example - the generated text file content

 

C# StreamReader and StreamWriter Example

 

Create a new console application project and add the following code.

 

 

 

using System;

using System.IO;

 

namespace StreamReaderCS

{

    class Program

    {

        static void Main(string[] args)

        {

            string path = @"c:\temp\MyTestFile.txt";

 

        try

        {

            if (File.Exists(path))

            {

                File.Delete(path);

            }

 

            Console.WriteLine("Writing some text to {0}", path);

            using (StreamWriter sw = new StreamWriter(path))

            {

                sw.WriteLine("This");

                sw.WriteLine("is some text");

                sw.WriteLine("written to test");

                sw.WriteLine("for reading...");

            }

 

            Console.WriteLine("Reading the previously written text..");

            using (FileStream fs = new FileStream(path, FileMode.Open))

            {

                using (StreamReader sr = new StreamReader(fs))

                {

                    while (sr.Peek() >= 0)

                    {

                        Console.WriteLine(sr.ReadLine());

                    }

                }

            }

        }

        catch (Exception e)

        {

            Console.WriteLine("The process failed: {0}", e.ToString());

        }

        }

    }

}

 

An output sample:

 

C# StreamReader and StreamWriter Example - a sample output

 

VB .NET StreamReader and StreamWriter Example

 

Create a new console application project and add the following code.

 

VB .NET StreamReader and StreamWriter Example - a new console application project creation  

 

Imports System

Imports System.IO

 

Module Module1

 

    Sub Main()

        Dim path As String = "c:\temp\MyTestVB.txt"

 

        Try

            Console.WriteLine("Deleting " + path + " if exists...")

            If File.Exists(path) Then

                File.Delete(path)

            End If

 

            Console.WriteLine("Then, creating a " + path + " file")

            Dim sw As StreamWriter = New StreamWriter(path)

            Console.WriteLine("Writing some text to the created file...")

            sw.WriteLine("This")

            sw.WriteLine("is some text")

            sw.WriteLine("written to test")

            sw.WriteLine("for reading...")

            sw.Close()

 

            Dim fs As FileStream = New FileStream(path, FileMode.Open)

            Console.WriteLine("Open " + path + " for reading")

            Dim sr As StreamReader = New StreamReader(fs)

            Console.WriteLine()

            Do While sr.Peek() >= 0

                Console.WriteLine(sr.ReadLine())

            Loop

            Console.WriteLine()

            Console.WriteLine("Closing...")

            sr.Close()

            fs.Close()

        Catch e As Exception

            Console.WriteLine("The process failed: {0}", e.ToString())

        End Try

    End Sub

End Module

 

An output sample:

 

VB .NET StreamReader and StreamWriter Example - a sample of output seen in the console mode

 

The created file can be seen under the temp folder.

 

VB .NET StreamReader and StreamWriter Example - the generated text files

 

 

 


 

< AuthenticatedStream, StreamWriter & StreamReader Class Examples | Main | BinaryReader & BinaryWriter Examples >