473,708 Members | 2,438 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read,write a file binary

How do I read, write a file binary?
I want to open, say, file.exe read it in to the program, then write it out
to file2.exe.
Like file copy, anyone have a code sample?
Nov 15 '05 #1
8 31007
Patrick,

File.Copy.

More general: BinaryReader/BinaryWriter.
Best regards,

Henrik Dahl

"Patrik Malmström" <po*****@popsor k.com> wrote in message
news:uB******** ******@TK2MSFTN GP12.phx.gbl...
How do I read, write a file binary?
I want to open, say, file.exe read it in to the program, then write it out
to file2.exe.
Like file copy, anyone have a code sample?

Nov 15 '05 #2
I've looked at them, but the exampel, wish I always use aren't very good
"Henrik Dahl" <Th************ **********@inet .uni2.dk> skrev i meddelandet
news:uA******** ******@TK2MSFTN GP11.phx.gbl...
Patrick,

File.Copy.

More general: BinaryReader/BinaryWriter.
Best regards,

Henrik Dahl

"Patrik Malmström" <po*****@popsor k.com> wrote in message
news:uB******** ******@TK2MSFTN GP12.phx.gbl...
How do I read, write a file binary?
I want to open, say, file.exe read it in to the program, then write it out to file2.exe.
Like file copy, anyone have a code sample?


Nov 15 '05 #3
Here ya go,

You will need to clean that up a little bit, but its a works...

-- Read

FileStream fs = new FileStream(@"C: \calc.exe", FileMode.OpenOr Create,
FileAccess.Read );
byte[] MyData= new byte[fs.Length];
fs.Read(MyData, 0, System.Convert. ToInt32(fs.Leng th));
fs.Close();

-- Write

FileStream fs1 = new FileStream(@"C: \calc1.exe", FileMode.OpenOr Create,
FileAccess.Writ e);
BinaryWriter bw = new BinaryWriter(fs 1);
bw.Write(MyData );
bw.Close();
fs1.Close();

"Patrik Malmström" <po*****@popsor k.com> wrote in message
news:ej******** ******@TK2MSFTN GP10.phx.gbl...
I've looked at them, but the exampel, wish I always use aren't very good
"Henrik Dahl" <Th************ **********@inet .uni2.dk> skrev i meddelandet
news:uA******** ******@TK2MSFTN GP11.phx.gbl...
Patrick,

File.Copy.

More general: BinaryReader/BinaryWriter.
Best regards,

Henrik Dahl

"Patrik Malmström" <po*****@popsor k.com> wrote in message
news:uB******** ******@TK2MSFTN GP12.phx.gbl...
How do I read, write a file binary?
I want to open, say, file.exe read it in to the program, then write it out to file2.exe.
Like file copy, anyone have a code sample?



Nov 15 '05 #4
Pramod,

Looks OK, but it could benefit from e.g. the following:

*) Use a BinaryReader for reading.
*) use using instead of instantiate ... Close. In this case disposing object
also work in case an exception should be thrown plus it's much more
declarative by nature.
*) Use (int) instead of System.Convert. ToInt32.

Additionally it may be advantageous to use the BitConverter class.
Best regards,

Henrik Dahl

"Pramod Anchuparayil" <pr**********@h otmail.com> wrote in message
news:e4******** *******@TK2MSFT NGP10.phx.gbl.. .
Here ya go,

You will need to clean that up a little bit, but its a works...

-- Read

FileStream fs = new FileStream(@"C: \calc.exe", FileMode.OpenOr Create,
FileAccess.Read );
byte[] MyData= new byte[fs.Length];
fs.Read(MyData, 0, System.Convert. ToInt32(fs.Leng th));
fs.Close();

-- Write

FileStream fs1 = new FileStream(@"C: \calc1.exe", FileMode.OpenOr Create,
FileAccess.Writ e);
BinaryWriter bw = new BinaryWriter(fs 1);
bw.Write(MyData );
bw.Close();
fs1.Close();

"Patrik Malmström" <po*****@popsor k.com> wrote in message
news:ej******** ******@TK2MSFTN GP10.phx.gbl...
I've looked at them, but the exampel, wish I always use aren't very good
"Henrik Dahl" <Th************ **********@inet .uni2.dk> skrev i meddelandet news:uA******** ******@TK2MSFTN GP11.phx.gbl...
Patrick,

File.Copy.

More general: BinaryReader/BinaryWriter.
Best regards,

Henrik Dahl

"Patrik Malmström" <po*****@popsor k.com> wrote in message
news:uB******** ******@TK2MSFTN GP12.phx.gbl...
> How do I read, write a file binary?
> I want to open, say, file.exe read it in to the program, then write
it out
> to file2.exe.
> Like file copy, anyone have a code sample?
>
>



Nov 15 '05 #5
Henrik Dahl <Th************ **********@inet .uni2.dk> wrote:
Looks OK, but it could benefit from e.g. the following:

*) Use a BinaryReader for reading.
What's the benefit there? I believe BinaryReader and BinaryWriter are
really there to make it easier to read and write primitives, not blocks
at a time.
*) use using instead of instantiate ... Close. In this case disposing object
also work in case an exception should be thrown plus it's much more
declarative by nature.
*) Use (int) instead of System.Convert. ToInt32.

Additionally it may be advantageous to use the BitConverter class.


Why?

Actually, I'd write it something like:

using (FileStream input = new FileStream (...))
{
using (FileStream output = new FileStream (...))
{
byte[] buffer = new byte[16384];
int len;
while ( (len=input.Read (buffer, 0, buffer.Length)) > 0)
{
output.Write (buffer, 0, len);
}
}
}

That way you don't need to read the whole lot into memory at a time.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #6
Thanks everyone!
Nov 15 '05 #7

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Henrik Dahl <Th************ **********@inet .uni2.dk> wrote:
Looks OK, but it could benefit from e.g. the following:

*) Use a BinaryReader for reading.
What's the benefit there? I believe BinaryReader and BinaryWriter are
really there to make it easier to read and write primitives, not blocks
at a time.


Yes, but often people have some order in their bytes where it's possible to
take advantage of these services.
*) use using instead of instantiate ... Close. In this case disposing object also work in case an exception should be thrown plus it's much more
declarative by nature.
*) Use (int) instead of System.Convert. ToInt32.

Additionally it may be advantageous to use the BitConverter class.
Why?


Same argument as before plus if the current architecture lets him to do it
at a higher abstration level.

Actually, I'd write it something like:

using (FileStream input = new FileStream (...))
{
using (FileStream output = new FileStream (...))
{
byte[] buffer = new byte[16384];
int len;
while ( (len=input.Read (buffer, 0, buffer.Length)) > 0)
{
output.Write (buffer, 0, len);
}
}
}

That way you don't need to read the whole lot into memory at a time.
I agree in your comment, but if this is the real thing to achieve I would
just use File.Copy. If the point just is to do the copying there's no point
in neither BinaryReader or BitConverter of course.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #8
Patrik,

Have you installed the .NET framework on the machine where you got this
error message?
Best regards,

Henrik Dahl

"Patrik Malmström" <po*****@popsor k.com> wrote in message
news:eq******** ******@TK2MSFTN GP12.phx.gbl...
I'll tried that code, ´but when I was trying to start calc1.exe, it sayed
that calc1.exe doesn't are a Win32 program, sorrym but I'm no good at C#.
"Pramod Anchuparayil" <pr**********@h otmail.com> skrev i meddelandet
news:e4******** *****@TK2MSFTNG P10.phx.gbl...
Here ya go,

You will need to clean that up a little bit, but its a works...

-- Read

FileStream fs = new FileStream(@"C: \calc.exe", FileMode.OpenOr Create,
FileAccess.Read );
byte[] MyData= new byte[fs.Length];
fs.Read(MyData, 0, System.Convert. ToInt32(fs.Leng th));
fs.Close();

-- Write

FileStream fs1 = new FileStream(@"C: \calc1.exe", FileMode.OpenOr Create,
FileAccess.Writ e);
BinaryWriter bw = new BinaryWriter(fs 1);
bw.Write(MyData );
bw.Close();
fs1.Close();

"Patrik Malmström" <po*****@popsor k.com> wrote in message
news:ej******** ******@TK2MSFTN GP10.phx.gbl...
I've looked at them, but the exampel, wish I always use aren't very good "Henrik Dahl" <Th************ **********@inet .uni2.dk> skrev i meddelandet news:uA******** ******@TK2MSFTN GP11.phx.gbl...
> Patrick,
>
> File.Copy.
>
> More general: BinaryReader/BinaryWriter.
>
>
> Best regards,
>
> Henrik Dahl
>
> "Patrik Malmström" <po*****@popsor k.com> wrote in message
> news:uB******** ******@TK2MSFTN GP12.phx.gbl...
> > How do I read, write a file binary?
> > I want to open, say, file.exe read it in to the program, then
write
it out
> > to file2.exe.
> > Like file copy, anyone have a code sample?
> >
> >
>
>



Nov 15 '05 #9

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

Similar topics

2
1631
by: blip | last post by:
Is this acceptable ? It seems too easy and too simple... #include<iostream> #include<fstream> #include<cstdlib> #include<string> struct Person{ char name ; int age ;
1
4305
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each time after routine process A, the text file is named "mytext.dat" in following format with "#####" as separator. The maximum entries of them is 5. When reaching the fifth entry, it will delete the very first entry.
16
3494
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 buffers. Basically, I want to be able to use all the standard read and write functions, but I want them to refer to memory locations, rather than disk files. I do not want to touch the legacy code. Does any one know of a library
4
2198
by: Feng Chun | last post by:
Hi, need help for this. In asp.net, when I do a load balancing on my website, which means there are 2 or more servers behind, one of my webpage needs to read/write a temp file in the server. But the client's first request may go to the first server, but the second request goes to the second server, so there is a chance that the request could not find the correct file to read/write. how to solve this issue?
7
4530
by: Mike Stephens | last post by:
I have a requirement to read / write to a proprietary data file. The fill will have no more than 10 records with no more than 10 fields. This is only for very simple data access. I want to completely avoid using ADO and XML. If someone can please point me in the right dirrection. Regards, Mike.
3
7158
by: I_AM_DON_AND_YOU? | last post by:
In my program I am using the notepad file to read/write data. I don't want that someone should be able to delete/change the contents of that file by opening that file in Notepad or other editor. Like there is a feature in Word to put password to open your document. I want to do something like that in Notepad file because I can't store the data in word file (in binary format) because I am using stream reader/write object. Is there any...
3
2471
by: erictran | last post by:
Gday everyone, Please help me. How to read and write a pointer of character to a file? Thanks in advance. Eric
5
18770
by: sheriff | last post by:
Dear friends, im a newbee for this forum and c++ im doing my MSc in Simulation Tech in mech. Engineering. My knowledge of c++ is very little which I had during my UG studies Long long ago .I am now forced to do some programming as a small part of my thesis work. Here goes my task and question. I want to read the text file and jus find the displacement old value and replace them with new value and write them in another file.........(In my...
4
4746
by: Bruno | last post by:
Hi! I have big .txt file which i want to read, process and write to another .txt file. I have done script for that, but im having problem with croatian characters (Š,Đ,Ž,Č,Ć). How can I read/write from/to file in utf-8 encoding? I read file with fileinput.input. thanks
0
8788
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8697
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
9290
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
9159
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
9001
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
5939
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();...
0
4454
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4713
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2097
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.