Writing Byte Array to Disk

G

Garth Wells

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
 
J

Jon Skeet [C# MVP]

Garth Wells said:
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.
 
C

Chris Mullins [MVP - C#]

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')
 
A

Arnshea

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(...)?
 
D

Doug Semler

Jon Skeet said:
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?
 
J

Jon Skeet [C# MVP]

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

I hadn't noticed the "new" (2.0) WriteAllBytes method :)
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

Chris Mullins said:
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 said:
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
 
A

Arnshea

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 :)
 
J

Jon Skeet [C# MVP]

Nicholas Paldino said:
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,Person> namesToPeople = 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 :)
 
N

Nicholas Paldino [.NET/C# MVP]

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

Jon Skeet [C# MVP]

Nicholas Paldino said:
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 :)
 
D

Doug Semler

Jon Skeet said:
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.


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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top