Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stream to text

Status
Not open for further replies.

richATrichardjones

Technical User
Sep 22, 2002
24
GB
Hello all
Sorry if this is a simple question but I am a java man and new to Visual Studio
I have a form where I have created a button which opens a file dialog. I want to input the file that is selected into another text box on the form. I cannot seem to convert from stream to text. How do i do this?
The code I using for this so far is:

==========================================

Stream* myStream;

OpenFileDialog* openFileDialog1 = new OpenFileDialog();

openFileDialog1->InitialDirectory = S"D:\\";
openFileDialog1->Filter = S"Media files (*dif)|*.DIF" ;
if(openFileDialog1->ShowDialog() == DialogResult::OK){
if((myStream = openFileDialog1->OpenFile())!= 0){
// This sort of thing below but this gives the
//object name.
// textBox1->Text = myStream->ToString();
myStream->Close();
}
}

==========================================

Thanks in advance
Richard

 
This is VisualC++ forum .... i think there is a Visual Java forum around here .....
 
The code you included looks like MFC, though since I don't use it, I can't be sure. :)
 
OK, I've never used .NET in my life, but reading over the MSDN, here's what I've learned.
OpenFileDialog::OpenFile() returns a Stream* pointer.
The MSDN example casts it to a FileStream* pointer like this
Code:
dynamic_cast<FileStream*>(dlgOpenFile->OpenFile());
The FileStream class has a Read() member that you can use to read in the data from the file, then just append it to your text box.
(I'm assuming that's what you're trying to do). If you're just trying to get the filename in the textbox, there's a FileName() member in the FileStream as well.
 
1. There is no need to use Stream if you need to take only the name of the selected file from the OpenDialog control
Example:
Code:
System::Windows::Forms::OpenFileDialog * openFileDialog2 = new System::Windows::Forms::OpenFileDialog();
openFileDialog2->InitialDirectory="C:\\temp";
openFileDialog2->Filter=S"Media files (*dif)|*.DIF" ;
if(openFileDialog2->ShowDialog() == DialogResult::OK)
{
	textBox1->Text = openFileDialog2->FileName;
}
2. Use Stream and OpenFile() when you know the structure of the file to open. See the Stream class for that.
3. If you want the file to be open by an associated process e.g. .txt file by notepad, .xls by Excel, .zip by Winzip, then add one more line to the above code:
Code:
 System::Windows::Forms::OpenFileDialog * openFileDialog2 = new System::Windows::Forms::OpenFileDialog();
openFileDialog2->InitialDirectory="C:\\temp";
openFileDialog2->Filter=S"Media files (*dif)|*.DIF" ;
if(openFileDialog2->ShowDialog() == DialogResult::OK)
{
  textBox1->Text = openFileDialog2->FileName;
  System::Diagnostics::Process::Start(textBox1->Text);
}
If you have a program associated with .dif then that program will open the selected file.
-obislavu-
 
Excellent Thanks a lot guys! I am just fiddling with visual c++ at the mo, hence the simplicity of my question

Thanks again
Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top