Partilhar via


Como escrever um arquivo de texto (C++/CLI)

O exemplo de código demonstra como criar um arquivo de texto e como gravar texto nele usando a classe StreamWriter, que é definida no namespace System.IO. O construtor StreamWriter leva o nome do arquivo a ser criado. Se o arquivo existir, ele será substituído (a menos que você passe Verdadeiro para o segundo argumento do construtor StringWriter).

O arquivo é então arquivado usando as funções Write e WriteLine.

Exemplo

// 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;
}

Consulte também

Outros recursos

E/S de arquivo e de fluxo

Programação do .NET no Visual C++