< C++ .NET System::IO - Files 1 | Main | C++ .NET System::IO - Files 3 >


 

 

Working with Files 2

 

 

What we have in this page?

 

 

  1. Using TextReader...continue

 

 

The following is the exercise for this part.

 

1.        Create a new Visual C++ CLR Console Application project named CppWriter.

 

Creating a new Visual C++ CLR Console Application project named CppWriter

 

2.        The TextWriter and file I/O classes are part of System::IO, so include a using declaration at the start of the program, CppWriter.cpp like this:

using namespace System::IO;

 

Including a using declaration at the start of the program, CppWriter.cpp

 

3.        In the main() function of the main project file, create a FileStream object to write to a file.

// Create a FileStream

try

{

   FileStream^ fs = gcnew FileStream(L"output.txt", FileMode::Create);

}

catch(System::Exception^ pe)

{

   Console::WriteLine(pe->ToString());

}

 

Creating a FileStream object to write to a file code

 

The FileStream constructor takes a file name and a mode. In this case, the file is going to be created if it doesn’t exist and overwritten if it does. We’ve used output.txt as the file name, but you can specify any path and file name you like for the new file. See the section “The FileStream Class” later in this module for more details on how to construct FileStream objects. The code is enclosed in a try block because a lot of things could go wrong when trying to open this file.

 

 

 

 

4.        Next, create a StreamWriter in the try block that uses the FileStream, as shown here:

// Create a StreamWriter

StreamWriter^ sw = gcnew StreamWriter(fs);

 

Creating a StreamWriter in the try block that uses the FileStream

 

The StreamWriter constructor takes a pointer to a Stream object as its one argument.

 

5.        You can now use the Write and WriteLine functions to output text to the file. Put the following lines inside the try block:

// Write some text

sw->WriteLine(L"First line of string");

sw->WriteLine(L"Second line of string");

sw->WriteLine(L"Third line of string");

6.        Make sure that all output is flushed to the file, and close the stream.

// Close up the file

sw->Flush();

sw->Close();

WriteLine performs buffered output, which means that it doesn’t necessarily write lines to the file every time you call the function. Instead, it maintains an internal buffer and writes the buffer to disk as necessary. One disk access per buffer is more efficient than writing individual lines, but you need to call Flush at the end of the code to make sure that output currently in the buffer is transferred to the file.

 

7.        Adding some informative string and send to the standard output. Add the following codes to see your program’s flow.

try

{

   // Create a FileStream

   Console::WriteLine("Creating a FileStream...");

   FileStream^ fs = gcnew FileStream(L"output.txt", FileMode::Create);

   // Create a StreamWriter

   Console::WriteLine("Creating a StreamWriter...");

   StreamWriter^ sw = gcnew StreamWriter(fs);

   // Write some text

   Console::WriteLine("Writing some texts...");

   sw->WriteLine(L"First line of string");

   sw->WriteLine(L"Second line of string");

   sw->WriteLine(L"Third line of string");

   // Close up the file

   Console::WriteLine("Flushing & closing FileStream...");

   sw->Flush();

   sw->Close();

}

catch(System::Exception^ pe)

{

   Console::WriteLine(pe->ToString());

}

 

Adding some informative string and send to the standard output

 

8.        Build and run the application.

 

A text file named output.txt should appear in the CppWriter project directory. The file contains the three lines of text written by the CppWriter application.

 

FileStream and StreamWriter program output example

 

The created text file under the project directory

 

 

Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

 

 


< C++ .NET System::IO - Files 1 | Main | C++ .NET System::IO - Files 3 >