-2

When I was trying to write text to a file in C++, nothing ever appeared in the area. I don't know if it was because I was using my own file extension, but when I do that in C#, it works.

I was using the code:

using namespace System::IO;

File::WriteAllText("C:/mypath/myfolder/mydocument.cra");

Is there anything I should be doing?

AlG
  • 14,697
  • 4
  • 41
  • 54
Joe
  • 104
  • 1
  • 13

2 Answers2

3

In C++, you can use fstream,

#include <fstream>

std::fstream fs;
fs.open ("test.txt", std::fstream::out );

fs << " some input";

fs.close();
CS Pei
  • 10,869
  • 1
  • 27
  • 46
2

The WriteAllText method takes a second parameter, the text to write to the file.

File::WriteAllText("C:\\mypath\\myfolder\\mydocument.txt", "Hello from C++/CLI!");

This API will replace the entire contents of the file with whatever you specify, so you'll have to build up the entire contents in memory before you write it.

David Yaw
  • 27,383
  • 4
  • 60
  • 93