Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual C++
Programming Guide
 How to: Write a Text File

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual C++ 
How to: Write a Text File 

The following code example demonstrates how to create a text file and write text to it using the StreamWriter class, which is defined in the System.IO namespace. The StreamWriter constructor takes the name of the file to be created. If the file exists, it is overwritten (unless you pass True as the second StringWriter constructor argument).

The file is then filed using the Write and WriteLine functions.

Example

// text_write.cpp
// compile with: /clr
using namespace System;
using namespace System::IO;

int main() 
{
   String^ fileName = "textfile.txt";

   StreamWriter^ sw = gcnew StreamWriter(fileName);
   sw->WriteLine("A text file is born!");
   sw->Write("You can use WriteLine");
   sw->WriteLine("...or just Write");
   sw->WriteLine("and do {0} output too.", "formatted");
   sw->WriteLine("You can also send non-text objects:");
   sw->WriteLine(DateTime::Now);
   sw->Close();
   Console::WriteLine("a new file ('{0}') has been written", fileName);

   return 0;
}

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker