473,320 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Append binary file 2 to binary file 1?

Greetings,

This seems like it should be simple but I can't figure out how to do this.

I just want to append binary file 2 on to the end of binary file 1.

Sample code please?

Thanks!

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Get freeware, learn things, make money.
http://www.vbmark.com
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Nov 17 '05 #1
14 11088
vbMark <no@email.com> wrote:
This seems like it should be simple but I can't figure out how to do this.

I just want to append binary file 2 on to the end of binary file 1.

Sample code please?


Open one FileStream to the file you wish to append to, using
FileMode.Append, and open another one to the file you wish to add to
the end, using FileMode.Open.

Allocate a buffer of, say, 32K, and repeatedly Read from the second
stream into the buffer and Write to the first stream however much data
you read.

See http://www.pobox.com/~skeet/csharp/readbinary.html for sample code
reading from buffers.

Don't forget to use "using" statements to dispose of your streams even
if exceptions are thrown.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
What should i call the functions named Factorial_ in this sample code:

class SomeClass {

...
public int Factorial(int f) {
if (f<0) throw SomeException("Some message.");
if (f==0 || f==1) return 1;
return Factorial_(f);
}

private int Factorial_(int f) {
if (f==1) return 1;
return f*Factorial_(f-1);
}
...
}

Calling it Factorial_ looks bad. Cannot call Factorial because it is the
name that the public method should have. What should it be?

telmo
Nov 17 '05 #3
Hi vbMark,
try this:

FileStream fs = new FileStream("c:\\somefile.abc", FileMode.Append);
BinaryWriter br = new BinaryWriter(fs);

This will allow you to append to existing files, or if the file does not
exist a new one will be created. So you can open one of your files and write
it onto the other binary file using the code from above.

Hope that helps
Mark R Dawson

"vbMark" wrote:
Greetings,

This seems like it should be simple but I can't figure out how to do this.

I just want to append binary file 2 on to the end of binary file 1.

Sample code please?

Thanks!

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Get freeware, learn things, make money.
http://www.vbmark.com
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Nov 17 '05 #4
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
vbMark <no@email.com> wrote:
This seems like it should be simple but I can't figure out how to do
this.

I just want to append binary file 2 on to the end of binary file 1.

Sample code please?


Open one FileStream to the file you wish to append to, using
FileMode.Append, and open another one to the file you wish to add to
the end, using FileMode.Open.

Allocate a buffer of, say, 32K, and repeatedly Read from the second
stream into the buffer and Write to the first stream however much data
you read.

See http://www.pobox.com/~skeet/csharp/readbinary.html for sample code
reading from buffers.

Don't forget to use "using" statements to dispose of your streams even
if exceptions are thrown.


I could not figure out how to use FIleStream.
I checked out the code on that site and was thoroughly confused.

I tried doing this:
StreamReader sr = new StreamReader("2.pdf");
StreamWriter sw = new StreamWriter("1.pdf",true);
string doc = "";
string line = sr.ReadLine();

while (line != null)
{
line = sr.ReadLine();
sw.WriteLine(line);
}
sr.Close();
sw.Close();

but ended up with 1.pdf being blank.

Am I close?

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Get freeware, learn things, make money.
http://www.vbmark.com
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Nov 17 '05 #5
vbMark <no@email.com> wrote:
This seems like it should be simple but I can't figure out how to do this.

I just want to append binary file 2 on to the end of binary file 1.

Sample code please?


Open one FileStream to the file you wish to append to, using
FileMode.Append, and open another one to the file you wish to add to
the end, using FileMode.Open.

Allocate a buffer of, say, 32K, and repeatedly Read from the second
stream into the buffer and Write to the first stream however much data
you read.

See http://www.pobox.com/~skeet/csharp/readbinary.html for sample code
reading from buffers.

Don't forget to use "using" statements to dispose of your streams even
if exceptions are thrown.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
vbMark <no@email.com> wrote in news:Xn********************@199.45.49.11:
Greetings,

This seems like it should be simple but I can't figure out how to do
this.

I just want to append binary file 2 on to the end of binary file 1.

Sample code please?

Thanks!


I'm close. The below code only gets a few of the first characters. What
do I need to get the whole thing?
FileStream fs = new FileStream("2.pdf", FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
string file2 = "";
file2 += r.ReadString();
fs = new FileStream("1.pdf", FileMode.Append);
BinaryWriter w = new BinaryWriter(fs);
w.Write( file2 );
w.Close();
fs.Close();

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Get freeware, learn things, make money.
http://www.vbmark.com
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Nov 17 '05 #7
vbMark <no@email.com> wrote:
I could not figure out how to use FIleStream.
I checked out the code on that site and was thoroughly confused.
In what way? Which bit was confusing?
I tried doing this:

StreamReader sr = new StreamReader("2.pdf");
StreamWriter sw = new StreamWriter("1.pdf",true);


Bad idea - StreamReaders are for *text* - PDFs are binary files.

Here's a program to append one file onto another - the first command
line parameter is the file to be appended to, the second command line
parameter is the file to append. There's currently no parameter
checking, of course...

using System;
using System.IO;

public class AppendFile
{
static void Main(string[] args)
{
using (Stream original = new FileStream(args[0],
FileMode.Append))
{
using (Stream extra = new FileStream(args[1],
FileMode.Open,
FileAccess.Read))
{
byte[] buffer = new byte[32*1024];

int blockSize;
while ( (blockSize = extra.Read
(buffer, 0, buffer.Length)) > 0)
{
original.Write (buffer, 0, blockSize);
}
}
}
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
vbMark <no@email.com> wrote:
This seems like it should be simple but I can't figure out how to do
this.

I just want to append binary file 2 on to the end of binary file 1.

Sample code please?


Open one FileStream to the file you wish to append to, using
FileMode.Append, and open another one to the file you wish to add to
the end, using FileMode.Open.

Allocate a buffer of, say, 32K, and repeatedly Read from the second
stream into the buffer and Write to the first stream however much data
you read.

See http://www.pobox.com/~skeet/csharp/readbinary.html for sample code
reading from buffers.

Don't forget to use "using" statements to dispose of your streams even
if exceptions are thrown.


I could not figure out how to use FIleStream.
I checked out the code on that site and was thoroughly confused.

I tried doing this:
StreamReader sr = new StreamReader("2.pdf");
StreamWriter sw = new StreamWriter("1.pdf",true);
string doc = "";
string line = sr.ReadLine();

while (line != null)
{
line = sr.ReadLine();
sw.WriteLine(line);
}
sr.Close();
sw.Close();

but ended up with 1.pdf being blank.

Am I close?

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Get freeware, learn things, make money.
http://www.vbmark.com
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Nov 17 '05 #9
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
vbMark <no@email.com> wrote:
I could not figure out how to use FIleStream.
I checked out the code on that site and was thoroughly confused.


In what way? Which bit was confusing?
I tried doing this:

StreamReader sr = new StreamReader("2.pdf");
StreamWriter sw = new StreamWriter("1.pdf",true);


Bad idea - StreamReaders are for *text* - PDFs are binary files.

Here's a program to append one file onto another - the first command
line parameter is the file to be appended to, the second command line
parameter is the file to append. There's currently no parameter
checking, of course...

using System;
using System.IO;

public class AppendFile
{
static void Main(string[] args)
{
using (Stream original = new FileStream(args[0],
FileMode.Append))
{
using (Stream extra = new FileStream(args[1],
FileMode.Open,
FileAccess.Read))
{
byte[] buffer = new byte[32*1024];

int blockSize;
while ( (blockSize = extra.Read
(buffer, 0, buffer.Length)) > 0)
{
original.Write (buffer, 0, blockSize);
}
}
}
}
}


That works great. Thanks.

FYI, it seems to prepend instead of append. But I can work with that.

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Get freeware, learn things, make money.
http://www.vbmark.com
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Nov 17 '05 #10
vbMark <no@email.com> wrote in news:Xn********************@199.45.49.11:
Greetings,

This seems like it should be simple but I can't figure out how to do
this.

I just want to append binary file 2 on to the end of binary file 1.

Sample code please?

Thanks!


I'm close. The below code only gets a few of the first characters. What
do I need to get the whole thing?
FileStream fs = new FileStream("2.pdf", FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
string file2 = "";
file2 += r.ReadString();
fs = new FileStream("1.pdf", FileMode.Append);
BinaryWriter w = new BinaryWriter(fs);
w.Write( file2 );
w.Close();
fs.Close();

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Get freeware, learn things, make money.
http://www.vbmark.com
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Nov 17 '05 #11
vbMark <no@email.com> wrote:
I could not figure out how to use FIleStream.
I checked out the code on that site and was thoroughly confused.
In what way? Which bit was confusing?
I tried doing this:

StreamReader sr = new StreamReader("2.pdf");
StreamWriter sw = new StreamWriter("1.pdf",true);


Bad idea - StreamReaders are for *text* - PDFs are binary files.

Here's a program to append one file onto another - the first command
line parameter is the file to be appended to, the second command line
parameter is the file to append. There's currently no parameter
checking, of course...

using System;
using System.IO;

public class AppendFile
{
static void Main(string[] args)
{
using (Stream original = new FileStream(args[0],
FileMode.Append))
{
using (Stream extra = new FileStream(args[1],
FileMode.Open,
FileAccess.Read))
{
byte[] buffer = new byte[32*1024];

int blockSize;
while ( (blockSize = extra.Read
(buffer, 0, buffer.Length)) > 0)
{
original.Write (buffer, 0, blockSize);
}
}
}
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #12
vbMark <no@email.com> wrote:

<snip>
That works great. Thanks.
Goodo.
FYI, it seems to prepend instead of append. But I can work with that.


No, it definitely appends. The first file specified is the file to be
appended *to*. The second file is appended to the end of the first
file. If you have files first.txt and second.txt, containing "hello "
and "there" respectively, and run the program with parameters
first.txt second.txt
then first.txt ends up being "hello there".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #13
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
vbMark <no@email.com> wrote:
I could not figure out how to use FIleStream.
I checked out the code on that site and was thoroughly confused.


In what way? Which bit was confusing?
I tried doing this:

StreamReader sr = new StreamReader("2.pdf");
StreamWriter sw = new StreamWriter("1.pdf",true);


Bad idea - StreamReaders are for *text* - PDFs are binary files.

Here's a program to append one file onto another - the first command
line parameter is the file to be appended to, the second command line
parameter is the file to append. There's currently no parameter
checking, of course...

using System;
using System.IO;

public class AppendFile
{
static void Main(string[] args)
{
using (Stream original = new FileStream(args[0],
FileMode.Append))
{
using (Stream extra = new FileStream(args[1],
FileMode.Open,
FileAccess.Read))
{
byte[] buffer = new byte[32*1024];

int blockSize;
while ( (blockSize = extra.Read
(buffer, 0, buffer.Length)) > 0)
{
original.Write (buffer, 0, blockSize);
}
}
}
}
}


That works great. Thanks.

FYI, it seems to prepend instead of append. But I can work with that.

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Get freeware, learn things, make money.
http://www.vbmark.com
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Nov 17 '05 #14
vbMark <no@email.com> wrote:

<snip>
That works great. Thanks.
Goodo.
FYI, it seems to prepend instead of append. But I can work with that.


No, it definitely appends. The first file specified is the file to be
appended *to*. The second file is appended to the end of the first
file. If you have files first.txt and second.txt, containing "hello "
and "there" respectively, and run the program with parameters
first.txt second.txt
then first.txt ends up being "hello there".

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

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

Similar topics

8
by: Tom | last post by:
Hi, I have a log file and of course I want to add the new information to the end of that log file. Unfortunately I always delete the old information and only append the current info. Right now...
2
by: gukn9700 | last post by:
The following program opens a text file and wants to append new text to it, but everytime I run it, it returns me with "open file fail!". Can anybody help? #include <fstream> #include...
8
by: b83503104 | last post by:
Hi, I want to append one (huge) file to another (huge) file. The current way I'm doing it is to do something like: infile = open (infilename, 'r') filestr = infile.read() outfile =...
2
by: vbMark | last post by:
Greetings, This seems like it should be simple but I can't figure out how to do this. I just want to append binary file 2 on to the end of binary file 1. Sample code please? Thanks!
0
by: Mad Scientist Jr | last post by:
can someone post code on how to * merge 2 mono wav files into a single stereo wav file (one file in the left channel one in the right) * append one wav file to another * merge 2 stereo wav...
2
by: ashwini.chaturvedi | last post by:
hi frnds, can anyone tell me how to append record/records in a binary file in vb.net thax
3
by: Freddy Coal | last post by:
Hi, I would like append strings to a binary file, but I don´t understand how make that. I try with: FileOpen(1, Folder_Trabajo & "\Toma_Trazas.FC", OpenMode.Append, OpenAccess.Write,...
4
by: BerlinBrown | last post by:
Is it possible to just build the binary content of a zip file. I want to create the content in memory (e.g. return binary data) and then get those byte strings representing the zip file? Is that...
4
by: saytri | last post by:
Hi guys! I am making a quiz. The questions are stored in a binary file. I made an option in the quiz where a user can add a question to the binary file. Altough this works, the problem is that...
2
by: kvnil | last post by:
Cheers, I've been struggling all day today with this, maybe you could give me some useful pointers. I would like to (elegantly) concatenate a string out of all node values in a map. I have to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.