473,320 Members | 1,946 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,320 software developers and data experts.

FileStream problem

The Win32 SDK had a great function called "ReadFile" that was a model of
simplicity. Now I'm starting to use the new .NET Framework, which is
supposed to be an improvement but the file reading is (as far as I can tell)
nowhere near as good.

FileStream fs=new FileStream(FName);
fs.BeginRead (Buffer,0,thisFile.Length,0,0);

The last two arguments (the two closing zeroes) are for "AsyncCallback" and
"stateObject." Microsoft's documentation does not clarify what these should
be. Can anybody clear this up?

I would like to read an entire (small) file of integers into a buffer in one
read. Can that be done? MSoft, you need to do more work on file reading,
it's not very useful in its current state.

Thanks very much for any help.

Nov 15 '05 #1
5 6400
Mike Robinson <ss*@spamfilterl.net> wrote:
The Win32 SDK had a great function called "ReadFile" that was a model of
simplicity. Now I'm starting to use the new .NET Framework, which is
supposed to be an improvement but the file reading is (as far as I can tell)
nowhere near as good.
I believe that's really just because you don't know the framework
particularly well.
FileStream fs=new FileStream(FName);
fs.BeginRead (Buffer,0,thisFile.Length,0,0);

The last two arguments (the two closing zeroes) are for "AsyncCallback" and
"stateObject." Microsoft's documentation does not clarify what these should
be. Can anybody clear this up?
If you read up on AsyncCallback or Asynchronous IO (the latter of which
is linked at the bottom of the page) you'll have much more idea.
I would like to read an entire (small) file of integers into a buffer in one
read. Can that be done? MSoft, you need to do more work on file reading,
it's not very useful in its current state.


It's perfectly fine in its current state.

Now, do you really need to read asynchronously? It doesn't sound like
you do - so just call Read instead of BeginRead. There's no guarantee
that FileStream will read the whole file in one go, however, so you may
need to loop round filling the buffer appropriately. It's not hard
though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2
Hi Mike,

Well, I would suggest that before posting you do a quick search on MSDN,
there you will see that there is two forms of using a FileStream , an
asynchronic and a syncronic ( this latter is the "normal" form ) , using
async allow you to continue to do other things while you are reading/writing
after the operation finish a method is called to inform of it. for this you
use BeginRead/EndRead methods
The other way is the old/plain and easy to use Read() method , that has the
signature that you expect ( look for it in the MSDN )

Of course you can read an entire file in once operation. just create a
buffer big enough.
Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Mike Robinson" <ss*@spamfilterl.net> wrote in message
news:Bq********************@giganews.com...
The Win32 SDK had a great function called "ReadFile" that was a model of
simplicity. Now I'm starting to use the new .NET Framework, which is
supposed to be an improvement but the file reading is (as far as I can tell) nowhere near as good.

FileStream fs=new FileStream(FName);
fs.BeginRead (Buffer,0,thisFile.Length,0,0);

The last two arguments (the two closing zeroes) are for "AsyncCallback" and "stateObject." Microsoft's documentation does not clarify what these should be. Can anybody clear this up?

I would like to read an entire (small) file of integers into a buffer in one read. Can that be done? MSoft, you need to do more work on file reading,
it's not very useful in its current state.

Thanks very much for any help.

Nov 15 '05 #3
Hi again Mike,

Here you will get some code for what you want, you will find a "problem"
basically that you want to fill a int[] but FileStream.Read expect a byte[]
therefore you need to convert it. I dig this code from an old file, hope it
helps you.

Pd: You will have to try it, I cannot assure it works cause I did not write
it in the first place.

FileStream fs = File.OpenRead( @"c:\Test\data.bin" );
int length = (int)fs.Length; //size in byte
int[] targetArray = new int [length /4]; an int is a 32 bit animal, aka 4
bytes long
byte[] buffer= new byte[length ];
fs.Read( buffer, 0, length );
System.Buffer.BlockCopy( buffer, 0, targetArray, 0, length );
Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Nov 15 '05 #4
"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> wrote:
Of course you can read an entire file in once operation. just create a
buffer big enough.


That doesn't guarantee that it'll read the whole file. From the docs:

<quote>
An implementation is free to return fewer bytes than requested even if
the end of the stream has not been reached.
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #5
"Ignacio Machin" <ignacio.machin AT dot.state.fl.us> wrote:
Yes you are right, the doc say that, now I wonder why ?


Well, it makes sense to read progressively in many situations - if
you've read everything the disk has given you, you (the OS/library
writer) might as well request more and have the disk spin and fetch it
while the caller is processing the first chunk.

I suspect it doesn't happen for smallish files, but I wouldn't be
surprised to see it happen for absolutely enormous files.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #6

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

Similar topics

5
by: GDL | last post by:
Hi, I'm using a FileStream as below: FileStream fs = new FileStream ( filePath, FileMode.Create, FileAccess.Write, FileShare.None
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....
3
by: Muki Rapp | last post by:
Hi! In the example below, once the media is full, the FileSteam.WriteByte throws an exception and the code is designed to handle it. However, when the GC is invoked, it calls the Finalize of...
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 =...
3
by: Loane Sharp | last post by:
Hi there I use the FileStream object to download a zip file over the internet to my local disk. The file downloads successfully, but when I attempt to unzip it, I'm told that the file is in use...
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,...
3
by: Hugh Janus | last post by:
Hi group, I am using a TCPStream together with a FileStream to send a file across a network. Everything works fine except for one thing. Always, at the end of the file there are several lines...
6
by: bonk | last post by:
I am trying to create a stream that writes text to a file and: - automatically creates a new file once the current file exceeds a certain size - makes it possible to be used by multiple threads...
2
by: Radek | last post by:
Hi, I have the following problem with FileStream. In this line: FileStream file = new FileStream(filePath, FileMode.Append); there is an exception FileNotFoundException. But for sure path and...
8
by: Andreas Zimmermann | last post by:
Hi, we just set up a SQL Server 2008 on Windows Server 2008 (configured as file / webserver, 16 GB memory, 4 dual core processors, 6 internal disks + 15 disc ISCSI array with overall almost 3 TB...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.