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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text to stream

Status
Not open for further replies.

pdbowling

Programmer
Mar 28, 2003
267
US
Hi, all. I thought this was easy, but it's not happening.

I just want to write some text to a stream in memory. Here is my code. No matter when I check the stream length, it is always zero. What is wrong?

Code:
string opfHeader = "--- Header text ---";            Stream opfStream = new MemoryStream();

long x = 0;
using (StreamWriter sw = new StreamWriter(opfStream))
{
    sw.WriteLine(opfHeader);
    x = opfStream.Length;

    for (int i = 0; i < 10; i++)
    {
        string line = i.ToString();
        sw.WriteLine(line);
    }
 
    x = opfStream.Length;
    x = sw.BaseStream.Length;
}

Gather or post content for free.
 
You forgot to call .Flush() on the StreamWriter before trying to access it.

Add sw.Flush(); right after the sw.WriteLine(opfHeader); line and it should work fine.

im in ur stakz, overflowin ur heapz!
 
Hmm, wow.

Thank you very much. I was using msdn to look at the streamWriter sample and they never had a sw.flush().

I appreciate your assistance. Thanks

Another quick question,

If I 'return' in the middle of a using clause, is the stream still closed by leaving the using clause or does it need to leave the using{ } brackets?

Code:
private long getlength(string myString)
{
    if(mystring.length == 0)
    {
        return 0;
    }
    Stream opfStream = new MemoryStream();
    using (StreamWriter sw = new StreamWriter(opfStream))
    {
        sw.WriteLine(myString);
        sw.flush();
        return opfStream.Length;
    }//Usually doesn't leave block before returning
}


P

Gather or post content for free.
 
With a using clause, the framework will run the dispose method when you leave the using code block. Technically, it will get closed, but it's a sloppy way to do it. I always recommend using the close method when you're done with it. Not only does it provide "closure" but also prevents mis-understandings if someone else is reading your code.

im in ur stakz, overflowin ur heapz!
 
my $.02
I would stick with using{} over explicit Close();Dispose();
Code:
using(Stream stream = new MemoryStream())
{
   //do something and return
}
translates to
Code:
Stream stream = new MemoryStream();
try
{
   //do something and return
}
finally
{
   stream.Close();
   stream.Dispose();
}
I find 'using' easier to read and understand and fewer key strokes. If you don't use a try/finally block or the using block then the stream will remain open if an exception is thrown before it is closed/disposed. MemoryStream may not be a big deal, but objects like FileStream (and database connections) definately need to be wrapped in a try/finally

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top