Read each line of a text file and write it out to the console. : StreamReader « File Directory « Visual C++ .NET






Read each line of a text file and write it out to the console.

 
#include "stdafx.h"
using namespace System;
using namespace System::IO;

int main()
{

   String^ filename = "textfile.txt";
   try
   {

      StreamReader^ sr2 = File::OpenText(filename);

      String^ line;
      while ((line = sr2->ReadLine()) != nullptr)
      {
         Console::WriteLine(line);
      }
   }
   catch(IOException^ e)
   {
       Console::WriteLine("Exception! {0}", e->Message );
   }
}

   
  








Related examples in the same category

1.Read text file with StreamReader
2.Read text file line by line
3.Creating a StreamReader class is with static methods of the File class