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

How can I read a file on disk into a memory stream?

Hello,
I have a file on disk called TEMP.ZIP and I would like to somehow get
this into a memory stream so I can eventually do this:

row["DOCUMENT"] = dataStream.ToArray()

However, I am not sure of the correct way to get it into a memory
stream.

Any help appreciated.

tia,
chance.

Apr 9 '07 #1
14 11607
chance,

You don't need a memory stream to load the file into a byte array. All
you have to do is open the filestream, and then read the contents into an
array. Since this is a file stream, you know the length in advance. You
can do this:

// The bytes.
byte[] bytes = null;
// The filestream.
using (FileStream fs = new FileStream(...))
{
// Allocate the bytes.
bytes = new byte[fs.Length];

// The number of bytes remaining, and the location to read from.
int bytesToRead = bytes.Length;
int readFrom = 0;

// The bytes read.
int bytesRead = 0;

// Read while there are bytes to be read.
while (bytesToRead 0)
{
// Read the data.
bytesRead = fs.Read(bytes, readFrom, bytesToRead);

// Decrement the remaining bytes by the number of bytes read.
bytesToRead = bytesToRead - bytesRead;

// Increment the next read operation.
readFrom = readFrom + bytesRead;
}
}

// Can use your array here.

I think you will find this much more efficient.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"chance" <ch****@crwmail.comwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...
Hello,
I have a file on disk called TEMP.ZIP and I would like to somehow get
this into a memory stream so I can eventually do this:

row["DOCUMENT"] = dataStream.ToArray()

However, I am not sure of the correct way to get it into a memory
stream.

Any help appreciated.

tia,
chance.

Apr 9 '07 #2
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:uL**************@TK2MSFTNGP04.phx.gbl...
chance,

You don't need a memory stream to load the file into a byte array. All
you have to do is open the filestream, and then read the contents into an
array. Since this is a file stream, you know the length in advance. You
can do this:
I also have similar problem, but I have an array of structures (with 2
int16,3 int32)
is there a way to read this directly from file into the struct array rather
than a byte[]?

at the moment im using a seperate byte array to read from the file,
then using the marshal class to do an unsafe copy from the byte to the
struct array.
this might be useful to the OP as well.

works fine but is there a direct way ? the amount of memory needed is quite
large.
I can get an unsafe ptr to the struct array ok, but the file needs a byte[]
is there a lower level unsafe read ?

I was looking at serialize but ive not managed to find much out about it
yet,
ive spent quite some time looking (I realy need to fix my help so its
online)

Its just to cache the csv file wich takes much longer to load.

Colin =^.^=
Apr 9 '07 #3
colin,

In this case, it might be easier to use unsafe code (if your solution
allows for that). Since your structures are simple (the structure itself
doesn't contain any pointers or references to anything else), you can read
the contents directly. Basically, you would open the file, and then make a
call to ReadFileEx (through the P/Invoke layer), passing a pointer to your
structure as the buffer.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"colin" <co*********@ntworld.NOSPAM.comwrote in message
news:d9****************@newsfe5-win.ntli.net...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:uL**************@TK2MSFTNGP04.phx.gbl...
>chance,

You don't need a memory stream to load the file into a byte array.
All you have to do is open the filestream, and then read the contents
into an array. Since this is a file stream, you know the length in
advance. You can do this:

I also have similar problem, but I have an array of structures (with 2
int16,3 int32)
is there a way to read this directly from file into the struct array
rather than a byte[]?

at the moment im using a seperate byte array to read from the file,
then using the marshal class to do an unsafe copy from the byte to the
struct array.
this might be useful to the OP as well.

works fine but is there a direct way ? the amount of memory needed is
quite large.
I can get an unsafe ptr to the struct array ok, but the file needs a
byte[]
is there a lower level unsafe read ?

I was looking at serialize but ive not managed to find much out about it
yet,
ive spent quite some time looking (I realy need to fix my help so its
online)

Its just to cache the csv file wich takes much longer to load.

Colin =^.^=

Apr 9 '07 #4
colin <co*********@ntworld.NOSPAM.comwrote:
You don't need a memory stream to load the file into a byte array. All
you have to do is open the filestream, and then read the contents into an
array. Since this is a file stream, you know the length in advance. You
can do this:

I also have similar problem, but I have an array of structures (with 2
int16,3 int32)
is there a way to read this directly from file into the struct array rather
than a byte[]?
Well, I'd make a static method (or constructor) in the struct which
reads it in from a stream. Then it's just a case of calling that
method/constructor the right number of times.
at the moment im using a seperate byte array to read from the file,
then using the marshal class to do an unsafe copy from the byte to the
struct array.
I'm personally wary of that kind of thing. I know this may be regarded
as heresy by some given how much less code is involved when using
automatic serialization or marshalling, but I prefer explict methods to
load or save objects. That makes the file format very clear, and it
makes the whole thing less fragile in the face of implementation
changes. On the other hand, if you have circular references etc, that
can be a significant issue which serialization takes care of but which
would need a lot of careful handling when serializing "manually".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 9 '07 #5
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
colin <co*********@ntworld.NOSPAM.comwrote:
You don't need a memory stream to load the file into a byte array.
All
you have to do is open the filestream, and then read the contents into
an
array. Since this is a file stream, you know the length in advance.
You
can do this:

I also have similar problem, but I have an array of structures (with 2
int16,3 int32)
is there a way to read this directly from file into the struct array
rather
than a byte[]?

Well, I'd make a static method (or constructor) in the struct which
reads it in from a stream. Then it's just a case of calling that
method/constructor the right number of times.
So using one call per member with IO.ReadInt16 etc ?
>
>at the moment im using a seperate byte array to read from the file,
then using the marshal class to do an unsafe copy from the byte to the
struct array.

I'm personally wary of that kind of thing. I know this may be regarded
as heresy by some given how much less code is involved when using
automatic serialization or marshalling, but I prefer explict methods to
load or save objects. That makes the file format very clear, and it
makes the whole thing less fragile in the face of implementation
changes. On the other hand, if you have circular references etc, that
can be a significant issue which serialization takes care of but which
would need a lot of careful handling when serializing "manually".
Wel yes it does detract from the point of using c#,
however as I said this is just to cache the data read from the CSV
(comma seperated variabl = text)
wich is very slow, rather than try to speed that up I decided to store it
temporarily in a binary format.
so any change in structure shouldnt be a problem.

theres also a LOT of data, any significant overhead per entry with function
calls could make it quite slow,
in particular any memory management overhead.

Colin =^.^=
Apr 9 '07 #6
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:uq****************@TK2MSFTNGP04.phx.gbl...
colin,

In this case, it might be easier to use unsafe code (if your solution
allows for that). Since your structures are simple (the structure itself
doesn't contain any pointers or references to anything else), you can read
the contents directly. Basically, you would open the file, and then make
a call to ReadFileEx (through the P/Invoke layer), passing a pointer to
your structure as the buffer.
ah many thanks, is this what your refering to ?
http://msdn2.microsoft.com/en-us/library/2d9wy99d.aspx
I dont need it to be asynchronous as its in a seperate thread anyway.

I couldnt find ReadFIle/Ex in any of the name spaces.

seems quite involved by comparison to reading to a byte[] and doing an
unsafe copy to struct[].

Colin =^.^=
Apr 9 '07 #7
colin <co*********@ntworld.NOSPAM.comwrote:
Well, I'd make a static method (or constructor) in the struct which
reads it in from a stream. Then it's just a case of calling that
method/constructor the right number of times.

So using one call per member with IO.ReadInt16 etc ?
Yup.
I'm personally wary of that kind of thing. I know this may be regarded
as heresy by some given how much less code is involved when using
automatic serialization or marshalling, but I prefer explict methods to
load or save objects. That makes the file format very clear, and it
makes the whole thing less fragile in the face of implementation
changes. On the other hand, if you have circular references etc, that
can be a significant issue which serialization takes care of but which
would need a lot of careful handling when serializing "manually".

Wel yes it does detract from the point of using c#,
however as I said this is just to cache the data read from the CSV
(comma seperated variabl = text)
wich is very slow, rather than try to speed that up I decided to store it
temporarily in a binary format.
so any change in structure shouldnt be a problem.
Fair enough. Just thought I'd give my 2p-worth.
theres also a LOT of data, any significant overhead per entry with function
calls could make it quite slow, in particular any memory management
overhead.
It's very unlikely that the processor cost would be particularly
significant compared with IO cost. You'd certainly need to profile it
before you could really say that having a lot of calls would make it
slow.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 9 '07 #8
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
colin <co*********@ntworld.NOSPAM.comwrote:
>theres also a LOT of data, any significant overhead per entry with
function
calls could make it quite slow, in particular any memory management
overhead.

It's very unlikely that the processor cost would be particularly
significant compared with IO cost. You'd certainly need to profile it
before you could really say that having a lot of calls would make it
slow.
I mean a lot of data, like 4gb, so I cant keep it in memory all the time
and re reading the CSV file is very slow indeed,
even though the files arnt much larger, and it probably gets read many
times.

Colin =^.^=
Apr 9 '07 #9
colin <co*********@ntworld.NOSPAM.comwrote:
It's very unlikely that the processor cost would be particularly
significant compared with IO cost. You'd certainly need to profile it
before you could really say that having a lot of calls would make it
slow.

I mean a lot of data, like 4gb, so I cant keep it in memory all the time
and re reading the CSV file is very slow indeed,
even though the files arnt much larger, and it probably gets read many
times.
Sure - and that's a perfectly good reason to create a binary
representation, but it's not a good reason to avoid the "manual"
serialization style.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 9 '07 #10
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:OL*************@TK2MSFTNGP05.phx.gbl...
Colin,

Attached you will find a sample file. It shows how to use unsafe code,
along with the ReadFile API function to read bytes directly from the
stream into your structure. This will not run, as there is no file stream
initialized, and there is no checking to see that the number of bytes read
from the file equals the number of bytes that the structure takes up in
memory (you really should put the call to ReadFile in a loop).

You can use ReadFileEx if you want to perform overlapped I/O, but I
doubt you are looking for this.
ah thats cool thank you very much :)
thats a fair bit simpler, saves going through the low level open and close
compared to the example I found.
il probably try to read the whole array at once, as theres ~ 100,000
entries.

Colin =^.^=
Apr 9 '07 #11
Colin,

You could try and do that, but remember, since it is a stream, you need
to make sure you loop as you read from it.

For example, if you know your file is 100 bytes long, and you ask to
read 100 bytes, you might get back anywhere UP TO 100 bytes. You have to
issue a read again for the remaining bytes.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"colin" <co*********@ntworld.NOSPAM.comwrote in message
news:9u*****************@newsfe5-win.ntli.net...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:OL*************@TK2MSFTNGP05.phx.gbl...
>Colin,

Attached you will find a sample file. It shows how to use unsafe
code, along with the ReadFile API function to read bytes directly from
the stream into your structure. This will not run, as there is no file
stream initialized, and there is no checking to see that the number of
bytes read from the file equals the number of bytes that the structure
takes up in memory (you really should put the call to ReadFile in a
loop).

You can use ReadFileEx if you want to perform overlapped I/O, but I
doubt you are looking for this.

ah thats cool thank you very much :)
thats a fair bit simpler, saves going through the low level open and close
compared to the example I found.
il probably try to read the whole array at once, as theres ~ 100,000
entries.

Colin =^.^=

Apr 9 '07 #12
See also:
System.IO.File.ReadAllBytes

Dave
Apr 10 '07 #13
That's even better!
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"D. Yates" <fo****@hotmail.comwrote in message
news:uf**************@TK2MSFTNGP04.phx.gbl...
See also:
System.IO.File.ReadAllBytes

Dave


Apr 10 '07 #14
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:u2**************@TK2MSFTNGP02.phx.gbl...
That's even better!
"D. Yates" <fo****@hotmail.comwrote in message
news:uf**************@TK2MSFTNGP04.phx.gbl...
>See also:
System.IO.File.ReadAllBytes

Dave

ooo runs off all excited to look ....

Colin =^.^=
Apr 10 '07 #15

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

Similar topics

11
by: Sebastian Krause | last post by:
Hello, I tried to read in some large ascii files (200MB-2GB) in Python using scipy.io.read_array, but it did not work as I expected. The whole idea was to find a fast Python routine to read in...
16
by: ben beroukhim | last post by:
I have huge number of legacy code which use standard files functions. I would like to pass a memory pointer rather than a FILE pointer. I am trying to use FILEs in the code to refer to memory...
18
by: JG | last post by:
Does anyone know a standard (or supported on Linux, Mac, Win32) way to clear a read stream buffer (standard ANSI C file stream)? I would even settle for a platform specific way of doing it. ...
0
by: Carlos Lozano | last post by:
Hi, I am working on a Flash player customization. The player is ready and works when loading the swf file from disk or url. That was pretty simple using a Flash.ocx wrapper. I would like to...
5
by: Mark Rae | last post by:
Hi, I'm writing a web app in VS.NET 2003, part of which allows users to download reports in the form of XML documents to their local machine for further processing. I'm using a 3rd-party Java...
10
by: Tibby | last post by:
I need to read/write not only text files, but binary as well. It seems like on binary files, it doesn't right the last 10% of the file. -- Thanks --- Outgoing mail is certified Virus...
3
by: Alex Shirley | last post by:
Hi Can you help me? I want to store a file of any description or format (from a hard disk) into memory in VB.NET (using ASP.NET). Then I want to use this datastructure in memory (containing...
13
by: =?Utf-8?B?Um9iS2lubmV5MQ==?= | last post by:
Hello, Here is a head scratcher for you all: We were wondering if it is possible to redirect file input to a place in memory (say a byte array for example). More specifically, if a function...
70
by: quickcur | last post by:
hi can anyone explain me to read image to memory from a url it is very easy in java but it is hard to find an complete solution in c/c++. Thanks,
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?
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.