473,387 Members | 1,510 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.

Writing Byte Array to Disk

I've got code that allows me to render the contents of a
byte array to the screen, but I need to write the contents
to disk. Suggestions?

Thanks
Oct 1 '07 #1
11 4498
On Oct 1, 6:11 pm, "Garth Wells" <nob...@nowhere.comwrote:
I've got code that allows me to render the contents of a
byte array to the screen, but I need to write the contents
to disk. Suggestions?

Thanks
FileStream.Write(...)?

Oct 1 '07 #2
Sure. Just write it to the disk. :)

// Create a 1 MB array, and fill with random stuff
var b = new byte[1024 * 1024];
(new Random()).NextBytes(b);

// Write array to disk
using (var f = File.Open("foo.bin", FileMode.CreateNew))
f.Write(b, 0, b.Length);

(I'm already addicted to the C# 3 syntax... I really like 'var')

--
Chris Mullins

"Garth Wells" <no****@nowhere.comwrote in message
news:ef**************@TK2MSFTNGP03.phx.gbl...
I've got code that allows me to render the contents of a
byte array to the screen, but I need to write the contents
to disk. Suggestions?

Thanks

Oct 1 '07 #3
Garth Wells <no****@nowhere.comwrote:
I've got code that allows me to render the contents of a
byte array to the screen, but I need to write the contents
to disk. Suggestions?
Use a FileStream, and call Write on it.

--
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
Oct 1 '07 #4
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Garth Wells <no****@nowhere.comwrote:
>I've got code that allows me to render the contents of a
byte array to the screen, but I need to write the contents
to disk. Suggestions?

Use a FileStream, and call Write on it.

OOC, what would the difference between that and File.WriteAllBytes() be?

--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?

Oct 2 '07 #5
Doug Semler <do********@gmail.comwrote:
Use a FileStream, and call Write on it.

OOC, what would the difference between that and File.WriteAllBytes() be?
I hadn't noticed the "new" (2.0) WriteAllBytes method :)

--
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
Oct 2 '07 #6
In this case, I don't like it. It adds ambiguity to the code, in the
sense that you don't know what type you are working with (you have to know
the return type of the Open method on the File class, which is
unreasonable). For projections on anonymous types in queries for LINQ, it
definitely makes sense, but in cases where you know the type, I think it is
a bad practice.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chris Mullins [MVP - C#]" <cm******@yahoo.comwrote in message
news:u8**************@TK2MSFTNGP04.phx.gbl...
Sure. Just write it to the disk. :)

// Create a 1 MB array, and fill with random stuff
var b = new byte[1024 * 1024];
(new Random()).NextBytes(b);

// Write array to disk
using (var f = File.Open("foo.bin", FileMode.CreateNew))
f.Write(b, 0, b.Length);

(I'm already addicted to the C# 3 syntax... I really like 'var')

--
Chris Mullins

"Garth Wells" <no****@nowhere.comwrote in message
news:ef**************@TK2MSFTNGP03.phx.gbl...
>I've got code that allows me to render the contents of a
byte array to the screen, but I need to write the contents
to disk. Suggestions?

Thanks


Oct 2 '07 #7
On Oct 1, 8:40 pm, "Doug Semler" <dougsem...@gmail.comwrote:
"Jon Skeet [C# MVP]" <sk...@pobox.comwrote in messagenews:MP*********************@msnews.microso ft.com...
Garth Wells <nob...@nowhere.comwrote:
I've got code that allows me to render the contents of a
byte array to the screen, but I need to write the contents
to disk. Suggestions?
Use a FileStream, and call Write on it.

OOC, what would the difference between that and File.WriteAllBytes() be?

--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?
Hehe, for those of us still stuck at 1.1, FileStream.Write() would
compile :)

Oct 2 '07 #8
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
I would agree that if you have the explicit type declaration on the
right side, then I would say var is ok on the left side. However, in the
case of a return value from a function, you are forcing a lookup for anyone
that has to maintain the code who might not know what the return value is.
Yes. I think it depends on what the method is though - File.Open is a
pretty common one, for instance. Various other methods are very obvious
in terms of what they return - Guid.NewGuid, for instance, although in
that case you'd only be saving a single character anyway.
As for book listings, I would say that it can be handy, but if used
improperly, or too gingerly, you aren't going to convey your points very
well. But I haven't written a book, so I can't say.
Oh it's certainly got to be used with care, yes. And only after
explaining what it does :)

--
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
Oct 2 '07 #9
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
In this case, I don't like it. It adds ambiguity to the code, in the
sense that you don't know what type you are working with (you have to know
the return type of the Open method on the File class, which is
unreasonable). For projections on anonymous types in queries for LINQ, it
definitely makes sense, but in cases where you know the type, I think it is
a bad practice.
In this particular case, I *might* agree - because it's not clear what
File.Open returns, although it's *fairly* obvious.

When you're using an object creation expression as the right hand side,
I like it a lot, particularly when the names get long:

var namesToPeople = new Map<string,Person>();

is clearer to me than

Map<string,PersonnamesToPeople = new Map<string,Person>();

There's no ambiguity at all - you can tell that namesToPeople is
definitely going to be of type Map<string,Person- but there's also no
redundancy, and less fluff to get in the way.

(It's particularly handy in listings for books, where horizontal space
is at a premium, but that's slightly different :)

--
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
Oct 2 '07 #10
I would agree that if you have the explicit type declaration on the
right side, then I would say var is ok on the left side. However, in the
case of a return value from a function, you are forcing a lookup for anyone
that has to maintain the code who might not know what the return value is.

As for book listings, I would say that it can be handy, but if used
improperly, or too gingerly, you aren't going to convey your points very
well. But I haven't written a book, so I can't say.

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

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
> In this case, I don't like it. It adds ambiguity to the code, in the
sense that you don't know what type you are working with (you have to
know
the return type of the Open method on the File class, which is
unreasonable). For projections on anonymous types in queries for LINQ,
it
definitely makes sense, but in cases where you know the type, I think it
is
a bad practice.

In this particular case, I *might* agree - because it's not clear what
File.Open returns, although it's *fairly* obvious.

When you're using an object creation expression as the right hand side,
I like it a lot, particularly when the names get long:

var namesToPeople = new Map<string,Person>();

is clearer to me than

Map<string,PersonnamesToPeople = new Map<string,Person>();

There's no ambiguity at all - you can tell that namesToPeople is
definitely going to be of type Map<string,Person- but there's also no
redundancy, and less fluff to get in the way.

(It's particularly handy in listings for books, where horizontal space
is at a premium, but that's slightly different :)

--
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

Oct 2 '07 #11
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
> I would agree that if you have the explicit type declaration on the
right side, then I would say var is ok on the left side. However, in the
case of a return value from a function, you are forcing a lookup for
anyone
that has to maintain the code who might not know what the return value
is.

Yes. I think it depends on what the method is though - File.Open is a
pretty common one, for instance. Various other methods are very obvious
in terms of what they return - Guid.NewGuid, for instance, although in
that case you'd only be saving a single character anyway.
> As for book listings, I would say that it can be handy, but if used
improperly, or too gingerly, you aren't going to convey your points very
well. But I haven't written a book, so I can't say.

Oh it's certainly got to be used with care, yes. And only after
explaining what it does :)

Oh god, don't tell me C# is becoming VB? <just kidding>

--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?

Oct 3 '07 #12

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

Similar topics

16
by: Ali | last post by:
Hi I want to write (or read) to a stream, but the data is not byte array I converted the data to byte array manually, but it is very slow, (becuse the data is very large) Is another way for this...
1
by: Greg Patrick | last post by:
My problem: I load an some assemblies (strong named) from a byte array using Assembly.Load(byte). They load fine. But one one of them is actually accessed, it's referenced assemblies can't be...
1
by: dsmith | last post by:
I need to write a data array out to disk, but unfortunately am running into performance issues. The function is essentially: private ushort theInt16Array = new ushort; public void...
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
3
by: Fireangel | last post by:
I want to cast a class into a byte array. I've seen some examples of this floating around, but they all have simple data members. What happens if I cast something that has a ArrayList or an...
2
by: Lars Netzel | last post by:
Here's what I need to do: Due to a bug in Crystal Reports ExportToStream() method I can't stream a Crystal Reports directly to the client as PDF and I must must use the Export() and put the the...
4
by: Richard506 | last post by:
If you take this Byte Arra Dim bytes() As Byte = { 207, 224, 135, 161, 253, 233, 111, 110, 99, 111, 100, 105, 110, 103, 32, 69, 120, 97, 109, 112, 108, 101 and write the byte array to disk...
6
by: John | last post by:
Hi I am trying to save settings of controls on my form to a file so I can read them back later and recreate the controls on the form. I have figured out how to go through all controls and get...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...

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.