-3

Is it possible to convert IO::Stream^ to std::string or char?

EDIT

//OpenFileDialog
IO::Stream^ myStream;
OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;

openFileDialog1->InitialDirectory = "c:\\";
openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1->FilterIndex = 2;
openFileDialog1->RestoreDirectory = true;

if(openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK){
    if ((myStream = openFileDialog1->OpenFile()) != nullptr){
        //code
        myStream->Close();
    }
}

//Button
Read(kai, converted_string_or_char); // I need to sent string or char which must be converted from IO::Stream^ myStream;

Write(kai);
richTextBox1->LoadFile("Results.txt", RichTextBoxStreamType::PlainText);

//Reading function
void Read(Sarasas *kai, string converted_string_or_char){
    string name, nr, city;
    int val, min, trukme;
    Abonentas abon;
    ifstream fd(converted_string_or_char);
    while(!fd.eof()){
        getline(fd, name, ',');
        fd >> nr >> city >> val >> min >> trukme;
        abon.Set(name, nr, city, val, min, trukme);
        ab->SetData(abon);
        fd >> ws;
    }
    fd.close();
}
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    What would this conversion do? How would you expect it to work? – David Heffernan Apr 21 '14 at 09:53
  • 1
    Sure (trivially, `Convert(x) = "a stream"`), the question is whether it's the conversion that you want. Could you give some example code with outputs? –  Apr 21 '14 at 09:53
  • I use OpenFileDialog and i have IO::Stream^ myStream; I need to convert it to string or char and use it to reading functions – user3556170 Apr 21 '14 at 09:56
  • 1
    That makes no sense at all to me. You need to describe precisely how you wish to convert a stream into a string, and indeed why you would do so. My guess is that you actually wish to use a file dialog to let the user select a file name, and then you wish to read the contents of the file with that name into a string. (http://stackoverflow.com/questions/23194086/open-file-dialog-c) – David Heffernan Apr 21 '14 at 09:58
  • 1
    FWIW this is not C++, it is C++/CLI – David Heffernan Apr 21 '14 at 10:06

1 Answers1

0

Assuming your file is encoded as a std::string would be (as in, Windows-1252 encoding), you should read the stream into a managed byte array and feed a pin_ptr to its first byte to std::string's constructor.

If the encodings differ, you should read the file as text (possibly using a StreamReader if that helps), then encode the resulting string as Windows-1252 (using System::Text::Encoding::Default->GetBytes(String^)) and then use the resulting byte array as above.

PS: You should also think on whether it is wise to use a std::string rather than an std::wstring.

Community
  • 1
  • 1
Medinoc
  • 6,577
  • 20
  • 42