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? 8 30969
Patrick,
File.Copy.
More general: BinaryReader/BinaryWriter.
Best regards,
Henrik Dahl
"Patrik Malmström" <po*****@popsork.com> wrote in message
news:uB**************@TK2MSFTNGP12.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?
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**************@TK2MSFTNGP11.phx.gbl... Patrick,
File.Copy.
More general: BinaryReader/BinaryWriter.
Best regards,
Henrik Dahl
"Patrik Malmström" <po*****@popsork.com> wrote in message news:uB**************@TK2MSFTNGP12.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?
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.OpenOrCreate,
FileAccess.Read);
byte[] MyData= new byte[fs.Length];
fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
-- Write
FileStream fs1 = new FileStream(@"C:\calc1.exe", FileMode.OpenOrCreate,
FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs1);
bw.Write(MyData);
bw.Close();
fs1.Close();
"Patrik Malmström" <po*****@popsork.com> wrote in message
news:ej**************@TK2MSFTNGP10.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**************@TK2MSFTNGP11.phx.gbl... Patrick,
File.Copy.
More general: BinaryReader/BinaryWriter.
Best regards,
Henrik Dahl
"Patrik Malmström" <po*****@popsork.com> wrote in message news:uB**************@TK2MSFTNGP12.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?
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**********@hotmail.com> wrote in message
news:e4***************@TK2MSFTNGP10.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.OpenOrCreate, FileAccess.Read); byte[] MyData= new byte[fs.Length]; fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length)); fs.Close();
-- Write
FileStream fs1 = new FileStream(@"C:\calc1.exe", FileMode.OpenOrCreate, FileAccess.Write); BinaryWriter bw = new BinaryWriter(fs1); bw.Write(MyData); bw.Close(); fs1.Close(); "Patrik Malmström" <po*****@popsork.com> wrote in message news:ej**************@TK2MSFTNGP10.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**************@TK2MSFTNGP11.phx.gbl... Patrick,
File.Copy.
More general: BinaryReader/BinaryWriter.
Best regards,
Henrik Dahl
"Patrik Malmström" <po*****@popsork.com> wrote in message news:uB**************@TK2MSFTNGP12.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? > >
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.com> http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.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.com> http://www.pobox.com/~skeet/ If replying to the group, please do not mail me too
Patrik,
Have you installed the .NET framework on the machine where you got this
error message?
Best regards,
Henrik Dahl
"Patrik Malmström" <po*****@popsork.com> wrote in message
news:eq**************@TK2MSFTNGP12.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**********@hotmail.com> skrev i meddelandet news:e4*************@TK2MSFTNGP10.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.OpenOrCreate, FileAccess.Read); byte[] MyData= new byte[fs.Length]; fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length)); fs.Close();
-- Write
FileStream fs1 = new FileStream(@"C:\calc1.exe", FileMode.OpenOrCreate, FileAccess.Write); BinaryWriter bw = new BinaryWriter(fs1); bw.Write(MyData); bw.Close(); fs1.Close(); "Patrik Malmström" <po*****@popsork.com> wrote in message news:ej**************@TK2MSFTNGP10.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**************@TK2MSFTNGP11.phx.gbl... > Patrick, > > File.Copy. > > More general: BinaryReader/BinaryWriter. > > > Best regards, > > Henrik Dahl > > "Patrik Malmström" <po*****@popsork.com> wrote in message > news:uB**************@TK2MSFTNGP12.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? > > > > > >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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 ;
|
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...
|
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...
|
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...
|
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...
|
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. ...
|
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
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
| | |