Hello.

How to convert System String to string or fstream ? What an idea. Open an open file with a file for reading ... And everything that opened is placed in fstream. The problem with placing in fstream.

fstream myStream; string str; if(openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) { System::IO::StreamReader ^ sr = gcnew System::IO::StreamReader(openFileDialog1->FileName); MessageBox::Show(sr->ReadLine()); myStream=sr; sr->Close(); } 
  • Something like variable = convert variable to string - Hdd out of range
  • Tried, can show by example? - Alerr 4:04 pm
  • one
    Why mix C ++ file streams and .NET streams? These are completely different types that should not be compatible. Use either fstream or StreamReader. - devoln
  • How to use StreamReader? It is very different from the 1st .... You can’t write to it so myStream >> prap; The problem is that I do not understand how to work with this StreamReader? and therefore I want to transfer it to fstream ... Or to anything ... - Alerr
  • one
    In std streams, the concepts of binary and text streams, as well as readers / writers, are mixed. In the .NET class, Stream is a binary stream that allows reading / writing of bytes, and readers / writers, for example, TextReader, XmlReader (and corresponding writers), are used to read / write text and any other content. - AlexeyM

1 answer 1

 string SystemToStl(String ^s) { using namespace Runtime::InteropServices; const char* ptr = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer(); return string(ptr); } 
  • one
    Hmm ... Is there a problem with the release of memory? The line selected with Marshal::StringToHGlobalAnsi() isn’t it necessary to free it with Marshal::FreeHGlobal() (as indicated on the documentation page)? - AlexeyM