473,387 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

FileStream.Write doing odd things...

Hi,

I'm having FileStream.Write behave rather oddly.

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

Expand|Select|Wrap|Line Numbers
  1.         Dim buffer As Byte()
  2.         Dim bytesRead As Integer
  3.         Dim fStreamIN As New FileStream("c:\a.txt", FileMode.Open, FileAccess.Read)
  4.         Dim fStreamOUT As New FileStream("c:\b.txt", FileMode.Append, FileAccess.Write)
  5.         ReDim buffer(fStreamIN.Length)
  6.  
  7.         bytesRead = fStreamIN.Read(buffer, 0, fStreamIN.Length)
  8.         MsgBox("Read bytes:" + Str(bytesRead) + " Expected:" + Str(fStreamIN.Length))
  9.         MsgBox("byte 1:" + Chr(buffer(0)) + vbCrLf + _
  10. "byte 2:" + Chr(buffer(1)) + vbCrLf + _
  11. "byte 3:" + Chr(buffer(2)) + vbCrLf)
  12.         fStreamOUT.Write(buffer, 0, buffer.Length)
  13.  
  14.         fStreamOUT.Flush()
  15.         fStreamIN.Close()
  16.         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
Aug 22 '07 #1
6 1736
I'm having FileStream.Write 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
Aug 22 '07 #2
Killer42
8,435 Expert 8TB
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?
Aug 23 '07 #3
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.
Aug 24 '07 #4
Killer42
8,435 Expert 8TB
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.
Aug 24 '07 #5
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>
Sep 2 '07 #6
Killer42
8,435 Expert 8TB
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).
Sep 2 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Kai Bohli | last post by:
Hi all ! This is my first day with Visual Studio and C#, and I'm trying to send "raw data" to the printer port. I found the SendFileToPrinter example below in a knowledge base at MS site It...
1
by: Shawn | last post by:
Hi. I'm using a FileStream (instead of just the path to the xml file) to load an XmlDocument. I'm doing this because I need to be able to prevent other processes to update the file I'm working on....
9
by: Tim | last post by:
Hi, I have a list of products in with images stored in a SQL Server 2000 DB. I need to be able to retrieve the images when a product is chose. I use the code below to get the image from the DB...
4
by: John Salerno | last post by:
In Murach's C#, it shows that StreamWriter and StreamReader both take a stream object as a parameter, and the book's examples of these two classes all first create a FileStream object. But I notice...
11
by: Dorsa | last post by:
HI, Could you please tell me the error in here. I am trying to open an XML file from a link. Response.Clear() Response.Expires = 0 Response.BufferOutput = False Response.ContentType =...
7
by: Nathan Sokalski | last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving,...
2
by: cj | last post by:
VB2005 I've opened files and read them using r = new io.streamreader("c:\thisfile.txt") line = r.readline Now I see an example where they are doing dim objopenfile as io.filestream = new...
4
by: DR | last post by:
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write...
0
by: Anthony Jones | last post by:
"DR" <softwareengineer98037@yahoo.comwrote in message news:OZoyDzR8IHA.1200@TK2MSFTNGP04.phx.gbl... of A simple experiment in .NET 2.0 and Windows XP shows that it does appear to flush on the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.