i'm making a programme in which there is a button to save new text file to disk, i make a programme but it not work help me what is the error i cant understand this

private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
            String^  add=textBox2->Text;
    String^ final=add+".txt";    
             ofstream save(final);

Edited by basit_3

3 Years
Discussion Span
Last Post by NathanOliver

i got this error message

1>c:\users\basit raza\documents\visual studio 2010\projects\handling\handling\Form1.h(201): error C2664: 'std::basic_ofstream<_Elem,_Traits>::basic_ofstream(const char *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'System::String ^' to 'const char *'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          No user-defined-conversion operator available, or

Why String^ to just a string?? change that.

@ritchieking: Presumably, basit_3 is working in C++/CLR (C++ for .Net, formerly called Managed C++), which despite what Microsoft says is in some ways a different language than Standard C++. The String class is a .NET Framework class, and the ^ operator makes it a 'managed' (e.g., garbage-collected) reference, IIUC. The two classes are not compatible, and all of the .Net classes that work with strings expect the String class, not the standard string.

@basit_3, if you were intending to write this as standard C++, rather than the .Net dialect, then ritchieking's advice is spot on. The System namespace and the String class are specific to Microsoft's .NET framework, and not a standard part of C++. I assume you know what you are doing, because your code is using .Net for the graphical user interface, but you ought to at least know that it is something specific to Windows.

To answer the question, I don't really know C++/CLR, but as I understand it, the std::ofstream class does not work with the .NET classes, or rather, the .NET classes don't work with unmanaged C++ I/O streams. I am fairly certain that there are .NET equivalents, but I don't know anything about them.

Edited by Schol-R-LEA


this code work
ofstream save("example.txt");

but i want name from the textBox instead of "example.txt"


@basit_3 you need to convert the String^ to a std::string if you need want to use std::ofstream you con use this to convert the string(source: MSDN):

// convert_system_string.cpp
// compile with: /clr
#include <string>
#include <iostream>
using namespace std;
using namespace System;

void MarshalString ( String ^ s, string& os ) {
   using namespace Runtime::InteropServices;
   const char* chars = 
      (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
   os = chars;

void MarshalString ( String ^ s, wstring& os ) {
   using namespace Runtime::InteropServices;
   const wchar_t* chars = 
      (const wchar_t*)(Marshal::StringToHGlobalUni(s)).ToPointer();
   os = chars;

int main() {
   string a = "test";
   wstring b = L"test2";
   String ^ c = gcnew String("abcd");

   cout << a << endl;
   MarshalString(c, a);
   c = "efgh";
   MarshalString(c, b);
   cout << a << endl;
   wcout << b << endl;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.