How to cast IO Stream to StreamWriter?

I

Ishmael

When I call
stream = saveFileDialog1->OpenFile();
the type returned is IO::Stream.

Now I would like to write a string to the stream, like this:
stream->Write("mystring");

Unfortunately, since 'stream' is of the type IO::Stream, there is no
'Write' method associated with it. This implies to me that maybe I
should use a stream of type StreamWriter, which does have a 'Write'
method. However, for some reason, I apparently can't convert between
the two, like this:
stream = safe_cast<System::IO::StreamWriter ^>(tmp);

So how do I write text to the file I just opened?

Thanks for your help!
 
I

Ishmael

Thanks for your reply. I'm afraid I'm still having problems. Here's
what I tried:
StreamWriter ^ sw(saveFileDialog1->OpenFile());
This gave me the following error:
cannot convert from 'System::IO::Stream ^' to
'System::IO::StreamWriter ^'

I also tried to set the BaseStream property:
stream->BaseStream = saveFileDialog1->OpenFile();
but apparently it is read-only.

Any ideas?

Thanks again.
 
I

Ishmael

Thanks for your reply. I'm afraid I'm still having problems. Here's
what I tried:
StreamWriter ^ sw(saveFileDialog1->OpenFile());
This gave me the following error:
cannot convert from 'System::IO::Stream ^' to
'System::IO::StreamWriter ^'

I also tried to set the BaseStream property:
stream->BaseStream = saveFileDialog1->OpenFile();
but apparently it is read-only.

Any ideas?

Thanks again.
 
I

Ishmael

Thanks for your reply. I'm afraid I'm still having problems. Here's
what I tried:
StreamWriter ^ sw(saveFileDialog1->OpenFile());
This gave me the following error:
cannot convert from 'System::IO::Stream ^' to
'System::IO::StreamWriter ^'

I also tried to set the BaseStream property:
stream->BaseStream = saveFileDialog1->OpenFile();
but apparently it is read-only.

Any ideas?

Thanks again.
 
I

Ishmael

Thanks. You're right, I'm new to Visual C++ and managed classes
And yes, I have looked at the documentation for the StreamWriter
class.

This reference looks like it should be helpful, but is not.
http://msdn.microsoft.com/en-us/library/6ka1wd3w.aspx
Here, a new StreamWriter object is created by passing in a string, the
filename. No mention of how to link it an IO::Stream.

These references are also deceptively straightforward:
http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx
http://msdn.microsoft.com/en-us/library/sfezx97z.aspx
At the crucial portion of the sample code, they say, ever so
helpfully: "// Code to write the stream goes here."

If you could hold my hand, I would greatly appreciate it.
Thanks.
 
F

Family Tree Mike

Ishmael said:
When I call
stream = saveFileDialog1->OpenFile();
the type returned is IO::Stream.

Now I would like to write a string to the stream, like this:
stream->Write("mystring");

Unfortunately, since 'stream' is of the type IO::Stream, there is no
'Write' method associated with it. This implies to me that maybe I
should use a stream of type StreamWriter, which does have a 'Write'
method. However, for some reason, I apparently can't convert between
the two, like this:
stream = safe_cast<System::IO::StreamWriter ^>(tmp);

So how do I write text to the file I just opened?

Thanks for your help!


Look at this link for a C++ example creating a StreamWriter from a Stream

http://msdn.microsoft.com/en-us/library/wtbhzte9.aspx
 
I

Ishmael

Thank you all for your help. I did, in fact, see the page you
referenced. Alas, in an effort to abide by the C standard I'm more
familiar with, in which all variables are declared at the top of a
function, I incorrectly tried something along the lines of:
Stream^ tmp;
StreamWriter^ stream;
.......
tmp = saveFileDialog1->OpenFile();
stream = StreamWriter(tmp);
This does not work for some reason.
However, the following does:
StreamWriter^ stream = gcnew StreamWriter(saveFileDialog1->OpenFile
());

Thanks again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top