473,385 Members | 1,597 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,385 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 11099
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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,...
0
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...

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.