Note: If you want to experience a complete C++ .NET/C++-CLI programming tutorial please jump to Visual C++ .NET programming tutorial.
Create a new CLR console application project and you might want to use SimpleWriterReaderCP as the project and solution names.
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.
You can try opening the created text file and in this case is shown below.
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:
Create a new console application project and add the following code.
![]() |
|
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:
The created file can be seen under the temp folder.