File IO using multiple writers in sequence

G

Guru

Michael D. Ober said:
Guru said:
Michael D. Ober said:
OK maybe I'm missing something obvious.

Look at the following code:
Using fs As New
FileStream(My.Computer.FileSystem.SpecialDirectories.Desktop &
"\junk.txt",
FileMode.Create, FileAccess.Write, FileShare.ReadWrite)
Using sw As New StreamWriter(fs,
System.Text.Encoding.ASCII)
sw.WriteLine("abc")
sw.WriteLine("def")
sw.WriteLine("ghi")
End Using
Using bw As New BinaryWriter(fs) 'throws an error right
here
bw.Write(CByte(&H4A)) 'J
bw.Write(CByte(&H4B)) 'K
bw.Write(CByte(&H4C)) 'L
bw.Write(CByte(&HA)) '[CR]
bw.Write(CByte(&HD)) '[LF]
bw.Write(CByte(&H4D)) 'J
bw.Write(CByte(&H4E)) 'K
bw.Write(CByte(&H4F)) 'L
bw.Write(CByte(&HA)) '[CR]
bw.Write(CByte(&HD)) '[LF]
End Using
End Using

It crashes on the line indicated with a "Stream not writeable"
exception.

I'm assuming the StreamWriter is closing the underlying FileStream
when it
is disposed. Where's the Filestream.Open command??

--
David Streeter
Synchrotech Software
Sydney Australia

You whacking great ignoramus. Do you get paid for writing code? Learn
about scope, you less than useless spaghetti-jockey; the freaking
streamwriter is derefenced as soon as the Using sw block terminates.
For crying out loud. BWAHAHAHAHAH!



Maybe you should look at the code again - the BinaryWriter isn't based
on the StreamWriter<BITCHSLAP>

Nitwit. The problem is, as I already asserted, caused by the Using sw
clause.

When the StreamWriter is disposed by the End Using on its head-long rush
into the total oblivion of Nothingness, it takes the FileStream along for
the ride; even Blind Bart can see that the BinaryWriter is trying to
reference the now obliterated FileStream.

Need proof? Yes? No? Don't know? Don't care? Considering suicide?
fs is a FileStream
{
sw is a StreamWriter based on fs
{
}
bw is a BinaryWriter based on fs
{
}
}

Maybe you should look at the code again - it is written in VB, not C.

That's your free cloo, clooboi. Don't take up coding for a living; you'll
starve.

The fix is simple. Change Using sw... into Dim sw... and remove the block
end. The intelligent reader, that would obviously not include you, will
note that the code now executes correctly; viz. the StreamWriter is not
disposed, therefore the FileStream is not disposed.

HTH, BegginerBoi.

Guru,

You're an ass. My pseudo code was<BITCHSLAP>

Note how you have been forced into a defensive posture.
By the way, since you are unable to post using your real name<BITCHSLAP>

Nutfuch. Guru _is_ my name, as in Guru Sandaramurthy.
you're not only an ass but also a coward.

What law of what land requires anyone to post with their real name, oh
pathologically obsessive one?

BTW, that you now feel the need to defend your pseudo-code, taken together
with your exceedingly limp-wristed attack on such an irrelevant point as
posting with ones 'real name', the fact of your obsequious and acquiescent
personality is pretty much stitched up.
 
M

Michael D. Ober

SurturZ said:
Well this code is more similar to the real world problem I'm having:

Imports System.IO

Module Module1
Sub Main()
Using s As New FileStream("C:\Temp\junk.tmp", FileMode.Create,
FileAccess.ReadWrite, FileShare.None)
Call WriteADog(s)
Call WriteADad(s)
Call WriteADog(s)
Call WriteADog(s)
Call WriteADad(s)
End Using
Console.WriteLine("Finished")
Console.ReadKey()
End Sub

Private Sub WriteADog(ByVal s As Stream)
Dim sw As New StreamWriter(s, System.Text.Encoding.ASCII)
sw.WriteLine("Dog")
End Sub

Public Sub WriteADad(ByVal s As Stream)
Dim bw As New BinaryWriter(s)
bw.Write(CByte(&HD))
bw.Write(CByte(&HA))
bw.Write(CByte(&HD))
End Sub
End Module

As you can see I have got rid of the Using blocks for the Writers.

Now, the code *does* work, but how do I now that Garbage Collection won't
fire in the middle of one of the subsequent Calls and close the stream? To
me, leaving the Writers undisposed is asking for an intermittent crash.

David,

You almost never have to call the Garbage Collector. As long as you have
references to objects created via standard dotNet library calls, the GC will
know that and not collect your objects.

Mike.
 
M

Michael D. Ober

Guru said:
Michael D. Ober said:
Guru said:
OK maybe I'm missing something obvious.

Look at the following code:
Using fs As New
FileStream(My.Computer.FileSystem.SpecialDirectories.Desktop &
"\junk.txt",
FileMode.Create, FileAccess.Write, FileShare.ReadWrite)
Using sw As New StreamWriter(fs,
System.Text.Encoding.ASCII)
sw.WriteLine("abc")
sw.WriteLine("def")
sw.WriteLine("ghi")
End Using
Using bw As New BinaryWriter(fs) 'throws an error right
here
bw.Write(CByte(&H4A)) 'J
bw.Write(CByte(&H4B)) 'K
bw.Write(CByte(&H4C)) 'L
bw.Write(CByte(&HA)) '[CR]
bw.Write(CByte(&HD)) '[LF]
bw.Write(CByte(&H4D)) 'J
bw.Write(CByte(&H4E)) 'K
bw.Write(CByte(&H4F)) 'L
bw.Write(CByte(&HA)) '[CR]
bw.Write(CByte(&HD)) '[LF]
End Using
End Using

It crashes on the line indicated with a "Stream not writeable"
exception.

I'm assuming the StreamWriter is closing the underlying FileStream
when it
is disposed. Where's the Filestream.Open command??

--
David Streeter
Synchrotech Software
Sydney Australia

You whacking great ignoramus. Do you get paid for writing code? Learn
about scope, you less than useless spaghetti-jockey; the freaking
streamwriter is derefenced as soon as the Using sw block terminates.
For crying out loud. BWAHAHAHAHAH!



Maybe you should look at the code again - the BinaryWriter isn't based
on the StreamWriter<BITCHSLAP>

Nitwit. The problem is, as I already asserted, caused by the Using sw
clause.

When the StreamWriter is disposed by the End Using on its head-long rush
into the total oblivion of Nothingness, it takes the FileStream along
for the ride; even Blind Bart can see that the BinaryWriter is trying to
reference the now obliterated FileStream.

Need proof? Yes? No? Don't know? Don't care? Considering suicide?

fs is a FileStream
{
sw is a StreamWriter based on fs
{
}
bw is a BinaryWriter based on fs
{
}
}

Maybe you should look at the code again - it is written in VB, not C.

That's your free cloo, clooboi. Don't take up coding for a living;
you'll starve.

The fix is simple. Change Using sw... into Dim sw... and remove the
block end. The intelligent reader, that would obviously not include you,
will note that the code now executes correctly; viz. the StreamWriter is
not disposed, therefore the FileStream is not disposed.

HTH, BegginerBoi.

Guru,

You're an ass. My pseudo code was<BITCHSLAP>

Note how you have been forced into a defensive posture.
By the way, since you are unable to post using your real name<BITCHSLAP>

Nutfuch. Guru _is_ my name, as in Guru Sandaramurthy.
you're not only an ass but also a coward.

What law of what land requires anyone to post with their real name, oh
pathologically obsessive one?

BTW, that you now feel the need to defend your pseudo-code, taken together
with your exceedingly limp-wristed attack on such an irrelevant point as
posting with ones 'real name', the fact of your obsequious and acquiescent
personality is pretty much stitched up.


In English speaking countries, very, very few people are named Guru.
Instead, guru is a label given someone who is extremely knowledgeable.
Given that background, I think you can understand my error on your name. As
for not using real names, take a look at most internet attacks on people -
the attackers rarely ever use their real names for fear of being slammed
back. This NG and others have had enough trolls who go by various fake
names that your posts combined with your name have convinced many of the
people in them that you are using a fake name to hide behind. Take a close
look at your posts and you will see that they tend to incite anger in other
people.

Mike.
 
G

Guru

Michael D. Ober said:
Guru said:
Michael D. Ober said:
OK maybe I'm missing something obvious.

Look at the following code:
Using fs As New
FileStream(My.Computer.FileSystem.SpecialDirectories.Desktop &
"\junk.txt",
FileMode.Create, FileAccess.Write, FileShare.ReadWrite)
Using sw As New StreamWriter(fs,
System.Text.Encoding.ASCII)
sw.WriteLine("abc")
sw.WriteLine("def")
sw.WriteLine("ghi")
End Using
Using bw As New BinaryWriter(fs) 'throws an error right
here
bw.Write(CByte(&H4A)) 'J
bw.Write(CByte(&H4B)) 'K
bw.Write(CByte(&H4C)) 'L
bw.Write(CByte(&HA)) '[CR]
bw.Write(CByte(&HD)) '[LF]
bw.Write(CByte(&H4D)) 'J
bw.Write(CByte(&H4E)) 'K
bw.Write(CByte(&H4F)) 'L
bw.Write(CByte(&HA)) '[CR]
bw.Write(CByte(&HD)) '[LF]
End Using
End Using

It crashes on the line indicated with a "Stream not writeable"
exception.

I'm assuming the StreamWriter is closing the underlying FileStream
when it
is disposed. Where's the Filestream.Open command??

--
David Streeter
Synchrotech Software
Sydney Australia

You whacking great ignoramus. Do you get paid for writing code? Learn
about scope, you less than useless spaghetti-jockey; the freaking
streamwriter is derefenced as soon as the Using sw block terminates.
For crying out loud. BWAHAHAHAHAH!



Maybe you should look at the code again - the BinaryWriter isn't based
on the StreamWriter<BITCHSLAP>

Nitwit. The problem is, as I already asserted, caused by the Using sw
clause.

When the StreamWriter is disposed by the End Using on its head-long
rush into the total oblivion of Nothingness, it takes the FileStream
along for the ride; even Blind Bart can see that the BinaryWriter is
trying to reference the now obliterated FileStream.

Need proof? Yes? No? Don't know? Don't care? Considering suicide?

fs is a FileStream
{
sw is a StreamWriter based on fs
{
}
bw is a BinaryWriter based on fs
{
}
}

Maybe you should look at the code again - it is written in VB, not C.

That's your free cloo, clooboi. Don't take up coding for a living;
you'll starve.

The fix is simple. Change Using sw... into Dim sw... and remove the
block end. The intelligent reader, that would obviously not include
you, will note that the code now executes correctly; viz. the
StreamWriter is not disposed, therefore the FileStream is not disposed.

HTH, BegginerBoi.


Guru,

You're an ass. My pseudo code was<BITCHSLAP>

Note how you have been forced into a defensive posture.
By the way, since you are unable to post using your real name<BITCHSLAP>

Nutfuch. Guru _is_ my name, as in Guru Sandaramurthy.
you're not only an ass but also a coward.

What law of what land requires anyone to post with their real name, oh
pathologically obsessive one?

BTW, that you now feel the need to defend your pseudo-code, taken
together with your exceedingly limp-wristed attack on such an irrelevant
point as posting with ones 'real name', the fact of your obsequious and
acquiescent personality is pretty much stitched up.


In English speaking countries, very, very few people are named Guru.
Instead, guru is a label

I speak perfect English, but where my parents came from (Uttar Pradesh), as
opposed to where I was born, grew up and live, Guru is a very, very common
name...
given someone who is extremely knowledgeable. Given that background, I
think you can understand my error on your name. As for not using real
names, take a look at most internet attacks on people - the attackers
rarely ever use their real names for fear of being slammed back.

LMAO - you are deluded.
...your posts ... tend to incite anger in other people.

<snicker>
 

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

Similar Threads

Reading EBCDIC Files. 9

Top