Reading file from OpenFileDialog.
-
private: System::Void menuOpenFile_Click(System::Object * sender, System::EventArgs * e) { StreamReader* InputStream; OpenFileDialog* openFileDialog1 = new OpenFileDialog(); openFileDialog1->InitialDirectory = S"c:\\" ; openFileDialog1->Filter = S"txt files (*.txt)|*.txt|All files (*.*)|*.*" ; openFileDialog1->FilterIndex = 2 ; openFileDialog1->RestoreDirectory = true ; if(openFileDialog1->ShowDialog() == DialogResult::OK) { if((InputStream = openFileDialog1->OpenFile())!= 0) { // Insert code to read the stream here. txtBoxSource->Clear(); String* lineOfText = InputStream->ReadLine(); while(lineOfText != NULL) { txtBoxSource->Text = String::Concat(txtBoxSource->Text, lineOfText, S"\r\n"); lineOfText = InputStream->ReadLine(); } InputStream->Close(); } } }
This program invoke a open file dialog. Now when I select a file in the open file dialog, how do I read it? I used StreadReader, but I got an error saying that it can't convert from System::IO.Stream to System::IO.StreamReader. I want to read the file and put it to the text box named txtBoxSource. Thanks ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented". -
private: System::Void menuOpenFile_Click(System::Object * sender, System::EventArgs * e) { StreamReader* InputStream; OpenFileDialog* openFileDialog1 = new OpenFileDialog(); openFileDialog1->InitialDirectory = S"c:\\" ; openFileDialog1->Filter = S"txt files (*.txt)|*.txt|All files (*.*)|*.*" ; openFileDialog1->FilterIndex = 2 ; openFileDialog1->RestoreDirectory = true ; if(openFileDialog1->ShowDialog() == DialogResult::OK) { if((InputStream = openFileDialog1->OpenFile())!= 0) { // Insert code to read the stream here. txtBoxSource->Clear(); String* lineOfText = InputStream->ReadLine(); while(lineOfText != NULL) { txtBoxSource->Text = String::Concat(txtBoxSource->Text, lineOfText, S"\r\n"); lineOfText = InputStream->ReadLine(); } InputStream->Close(); } } }
This program invoke a open file dialog. Now when I select a file in the open file dialog, how do I read it? I used StreadReader, but I got an error saying that it can't convert from System::IO.Stream to System::IO.StreamReader. I want to read the file and put it to the text box named txtBoxSource. Thanks ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".Hai Alex, you can solve the problem with this help of FileStram class FileStream* fs; StreamReader* InputStream; OpenFileDialog* openFileDialog1 = new OpenFileDialog(); if(openFileDialog1->ShowDialog() == DialogResult::OK) { fs=new FileStream(openFileDialog->FileName,FileMode::Open); InputStream=new StreamReder(fs); //Then done your reading using InputStream } NB: please include using namespace System::IO;