Reputation: 81
I want give a string as a argument to the Stream Writer writeline method in visual c++
StreamWriter^ sw = gcnew StreamWriter("Positive Sample.txt");
string Loc = "blabla";
sw->WriteLine(Loc);
It generating a error - no instance of overload function match the argument list
Upvotes: 2
Views: 2922
Reputation: 22157
WriteLine
method accepts CLI's String, and not std's string.
StreamWriter^ sw = gcnew StreamWriter("Positive Sample.txt");
String^ Loc = "blabla";
sw->WriteLine(Loc);
You can use System::Runtime::InteropServices::Marshal::PtrToStringAnsi
to marshal from C string to String
, or you can pass C string into String's constructor:
string Loc = "blabla";
String^ strLoc = gcnew String(Loc.c_str());
EDIT
As Ben pointed out in the comments, you should use marshal_as
instead PtrToStringAnsi
:
Example from here (inverse operation could be found here)
// marshal_as_test.cpp
// compile with: /clr
#include <stdlib.h>
#include <string.h>
#include <msclr\marshal.h>
using namespace System;
using namespace msclr::interop;
int main() {
const char* message = "Test String to Marshal";
String^ result;
result = marshal_as<String^>( message );
return 0;
}
Upvotes: 3