FileStream.Write doing odd things...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • andycee
    New Member
    • Oct 2006
    • 2

    #1

    FileStream.Write doing odd things...

    Hi,

    I'm having FileStream.Writ e behave rather oddly.

    The code below should take file a.txt and append it to file b.txt :

    Code:
            Dim buffer As Byte()
            Dim bytesRead As Integer
            Dim fStreamIN As New FileStream("c:\a.txt", FileMode.Open, FileAccess.Read)
            Dim fStreamOUT As New FileStream("c:\b.txt", FileMode.Append, FileAccess.Write)
            ReDim buffer(fStreamIN.Length)
    
            bytesRead = fStreamIN.Read(buffer, 0, fStreamIN.Length)
            MsgBox("Read bytes:" + Str(bytesRead) + " Expected:" + Str(fStreamIN.Length))
            MsgBox("byte 1:" + Chr(buffer(0)) + vbCrLf + _
    "byte 2:" + Chr(buffer(1)) + vbCrLf + _
    "byte 3:" + Chr(buffer(2)) + vbCrLf)
            fStreamOUT.Write(buffer, 0, buffer.Length)
    
            fStreamOUT.Flush()
            fStreamIN.Close()
            fStreamOUT.Close()
    I test with :

    a.txt "abc"
    b.txt "<This is the Orig>"

    Now when I run this code, the characters in a.txt are read correctly to buffer, but the file b.txt contains :

    "<This is the Orig>扡c"

    What on earth is going on ???

    cheers
  • JonJacobs
    New Member
    • Aug 2007
    • 22

    #2
    Originally posted by andycee
    I'm having FileStream.Writ e behave rather oddly.
    You need to flush and close then IN stream before you write to the OUT stream, otherwise you might not get everything you need into your buffer.

    I modified your code accordingly, and it worked fine for me, when I made that change.

    HTH

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by JonJacobs
      I modified your code accordingly, and it worked fine for me, when I made that change.
      Did you test whether the problem occurred before making the change?

      Comment

      • JonJacobs
        New Member
        • Aug 2007
        • 22

        #4
        Originally posted by Killer42
        Did you test whether the problem occurred before making the change?
        Failing to close the stream before using the buffer will have variable results, but it is definitely the source of the problem.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by JonJacobs
          Failing to close the stream before using the buffer will have variable results, but it is definitely the source of the problem.
          Ok. I never use stream, just wanted to double-check we weren't overlooking something.

          By the way, you wouldn't actually need to flush an input stream, would you? Unless the meaning of "flush" has changed in the last few years.

          Comment

          • JonJacobs
            New Member
            • Aug 2007
            • 22

            #6
            Originally posted by Killer42
            Ok. I never use stream, just wanted to double-check we weren't overlooking something.

            By the way, you wouldn't actually need to flush an input stream, would you? Unless the meaning of "flush" has changed in the last few years.
            True enough. I flush everything, just in case, since I am new to VB, and I am paranoid. <g>

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Originally posted by JonJacobs
              True enough. I flush everything, just in case ...
              Ok, thanks. Just wanted to check, in case I was just behind the times (as in so many things).

              Comment

              Working...