By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

Writing Byte Array to Disk

P: n/a
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
Share this Question
Share on Google+
11 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.