Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 7th, 2008, 03:55 AM
Authorized User
 
Join Date: Aug 2007
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to yukijocelyn
Default Binary writer equivalent

Hi

I would like to ask if whether there is a equivalent code that uses binary writer to do the following codes?

Code:
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
     int offset = 0;
     int count = (int)fs.Length;
     buffer = new byte[count];
     while (count > 0)
     {
          int bytesRead = fs.Read(buffer, offset, count);
          offset += bytesRead;
          count -= bytesRead;
     }
}
Thanks!

 
Old August 7th, 2008, 04:17 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Hi

I don't understand what you are trying to accomplish. The above code writes an array of bytes to a file, while BinaryWriter is designed to write .Net primitives (e.g. String, Int32, Double, etc) to a stream.

A BinaryWriter will write to any stream (which includes a FileStream).

Does that help?

/- Sam Judson : Wrox Technical Editor -/
 
Old August 7th, 2008, 05:57 AM
Authorized User
 
Join Date: Aug 2007
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to yukijocelyn
Default

Hi!

Thanks for the reply.
I am doing a client server which I have to like attach files and send it over to the server, which the server will receive it and save it.

I am currently doing it through TCP connection, however I still have to slice the data up to pass through the network because my files tend to be big.

At the server, I am using Binary Writer to write back into a file, eg: if I send a image file of file name "image.jpg", the server will receive this "image.jpg" and save it into its own directory.

At the client side, I am using FileStream to extract the data of the file into byte array. But at the server side when all the different segments are received, it will use Binary Writer to save into a file in its own directory.

However I am experiencing that the file is corrupted and the image cannot be seen. I was wondering if whether will there be compatibility problems between FileStream & BinaryWriter. So I'm asking whether is there a BinaryWriter equivalent of the codes using FileStream.

Thanks!

 
Old August 7th, 2008, 06:04 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Like I said - BinaryWriter is for writing strings and integers to a stream.

You should have an array of bytes at one end, and an array of bytes at the other end - so BinaryWriter shouldn't come into it.

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Where is the writer,help! bimawen Other Programming Languages 0 April 21st, 2008 01:52 AM
A really good report writer....? seananderson SQL Language 0 August 13th, 2007 04:50 AM
Render Via Binary Writer rwiethorn BOOK: Professional SQL Server 2005 Reporting Services ISBN: 0-7645-8497-9 3 August 2nd, 2007 02:44 AM
.Transform(xDoc, Nothing, writer) reidcor BOOK: ASP.NET Website Programming Problem-Design-Solution 4 August 27th, 2004 01:36 PM
How do I use the report writer in VB.NET dkomo VB.NET 2002/2003 Basics 1 May 6th, 2004 11:14 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.