473,403 Members | 2,071 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,403 software developers and data experts.

The size of buffer

ad
I new a buffer for read file:
How to detriminate the BolockSize when new the buffer?
-----------------------------------------------------------------------------------------------------------------------------------------
int BlockSize=????
System.IO.FileStream fsRead = new System.IO.FileStream("c:\Backup.bck",
System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] buffer = new byte[BlockSize];
System.Int32 size = fsRead.Read(buffer, 0, buffer.Length);
Nov 17 '05 #1
1 2522
Hi ad,
generally when you do not know the size of the file in advance, you can
just assign some arbritary size to your buffer and keep looking in a while
loop calling read until the FileStream.Read function returns a value < 0 at
which point you know there is no more data.

In the code example you gave, you know exactly the size of the file you are
reading because you can use the FileInfo object to get the size of the file
in bytes i.e.

System.IO.FileInfo fi = new FileInfo(@"c:\backup.bck");
long fileSize = fi.Length;

I would not make your buffer equal to the size of your file though, make it
some arbitrary size like 500K, for example, and keep looping until all the
data is read, this way if you are reading a large file or you want to cancel
the operation you still have chance to cancel the read operation by putting
extra code in your while loop to do so i.e.

System.IO.FileInfo fi = new FileInfo(@"c:\backup.bck");
long fileSize = fi.Length;

//have some boolean that can be set i.e. when the user clicks on
//a cancel button this would turn to false
bool keepReading = true;

byte[] arrData = new byte[500 * 1024];

long remainingData = fileSize;
int dataRead;
int offset = 0;

while(remainingData > 0 && keepReading)
{
dataRead = fs.Read(arrData, offset, remainingData);

if(dataRead <= 0)
{
//we are done
break;
}

remainingData -= dataRead;
}

Hope that helps
Mark R Dawson

"ad" wrote:
I new a buffer for read file:
How to detriminate the BolockSize when new the buffer?
-----------------------------------------------------------------------------------------------------------------------------------------
int BlockSize=????
System.IO.FileStream fsRead = new System.IO.FileStream("c:\Backup.bck",
System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] buffer = new byte[BlockSize];
System.Int32 size = fsRead.Read(buffer, 0, buffer.Length);

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Raja | last post by:
How to know the buffer size and increase buffer size in c++.
13
by: Sharon | last post by:
I'm using TcpClient and getting the Stream by: TcpClient tcpclnt = new TcpClient(); . . . Stream stm = tcpclnt.GetStream(); Now, when I'm trying to send a big buffer via stm.Write(...) I get an...
11
by: Stephan Steiner | last post by:
Hi Generally, FileInfo fi = new FileInfo(path); long size = fi.Length; gets you the length of a file in bytes. However, when copying files, even while the copy operation is still in...
1
by: Jon Finch | last post by:
Hi All this is a follow up to a post I did a few days back regarding reading a font from an assembly into a memory font object. The only thing I am stuck on is getting the actual memory size of a...
2
by: Macca | last post by:
Hi, My application uses an asynchronous socket server. The question I have is what i should set my socket server buffer size to. I will know the size of each data packet sent across the...
43
by: Frodo Baggins | last post by:
Hi all, We are using strcpy to copy strings in our app. This gave us problems when the destination buffer is not large enough. As a workaround, we wanted to replace calls to strcpy with strncpy....
9
by: Notebooker | last post by:
Hello, I'm an intermediate noob reading-in data from ascii-file using an ifstream object. I have specified a c-style string buffer with size of type size_t and I am specifying to use this...
36
by: James Harris | last post by:
Initial issue: read in an arbitrary-length piece of text. Perceived issue: handle variable-length data The code below is a suggestion for implementing a variable length buffer that could be used...
4
by: Zytan | last post by:
This may be the dumbest question of all time, but... When I set the packet size, does it mean ALL packets are that size, no matter what? Let's say the packet size is 8KB, and I send a 5 byte...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.