473,796 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4524
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.Writ e(...)?

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()).NextB ytes(b);

// Write array to disk
using (var f = File.Open("foo. bin", FileMode.Create New))
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******** ******@TK2MSFTN GP03.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.co m>
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.co mwrote in message
news:MP******** *************@m snews.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.WriteAllBy tes() 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********@gma il.comwrote:
Use a FileStream, and call Write on it.

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

--
Jon Skeet - <sk***@pobox.co m>
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.co m

"Chris Mullins [MVP - C#]" <cm******@yahoo .comwrote in message
news:u8******** ******@TK2MSFTN GP04.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()).NextB ytes(b);

// Write array to disk
using (var f = File.Open("foo. bin", FileMode.Create New))
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******** ******@TK2MSFTN GP03.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...@gma il.comwrote:
"Jon Skeet [C# MVP]" <sk...@pobox.co mwrote in messagenews:MP* *************** *****@msnews.mi crosoft.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.WriteAllBy tes() 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.Writ e() would
compile :)

Oct 2 '07 #8
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard .caspershouse.c omwrote:
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.co m>
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.c omwrote:
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,Pers on>();

is clearer to me than

Map<string,Pers onnamesToPeople = new Map<string,Pers on>();

There's no ambiguity at all - you can tell that namesToPeople is
definitely going to be of type Map<string,Pers on- 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.co m>
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

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

Similar topics

16
9115
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 kind of writing and reading best regard Ali.
1
15960
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 found on disk and I get a TargetInvocationExeption. For example, let's say I have a C# win forms application called Prog. Prog.exe uses Assembly.Load(byte) to load two C# class library assemblies,
1
4093
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 Save(BinaryWriter bw) { /// Far too slow: // for (int row = 0; row < Rows; row++) // {
2
6861
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 replace just one item. I know I can get the entire array, then save the whole thing (with a for loop and if statements so that the changed data will be saved), but it seems like a lot of unnecessary reading and writing. Is there a way to directly save...
3
1551
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 Array?? Does it loop through the array and cast them all and I end up with a big array (This is what I hope will happen)? Or does it simple cast the pointer address and I end up with a small array?? Related question: How do I make sure everything...
2
3412
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 PDF on disk first and stream to the client. That's just how it is. Question: So I need to let the aspx user have write permissions to disk.. NOT GOOD. How do I solve this? Can I somehow write the PDF to disk in another thread with other writes?...
4
2009
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 FileOpen(2, "f:\aaapicture_album\result.txt", OpenMode.Binary FilePut(2, bytes FileClose(2 then convert it into a string --->
6
1607
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 their properties (see code below). What I am not sure is; how and what type of file format I need to save the info to and how to read it back. Would appreciate any help on that. A code snippet would be very much appreciated. Many thanks
0
9535
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10465
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10242
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10021
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.