将IO :: Stream ^转换为std :: string或char c ++

时间:2014-04-21 09:48:53

标签: c++-cli

是否可以将IO::Stream^转换为std::stringchar

修改

//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();
}

1 个答案:

答案 0 :(得分:0)

假设您的文件被编码为std::string(如Windows-1252编码),您应该read the stream into a managed byte array并将pin_ptr的第一个字节输入{{1}构造函数。

如果编码不同,您应该将文件读作文本(如果有帮助,可能使用std::string),然后将结果字符串编码为Windows-1252(使用StreamReader),然后使用结果如上所述的字节数组。

PS:您还应该考虑使用System::Text::Encoding::Default->GetBytes(String^)而不是std::string是否明智。