Append binary file 2 to binary file 1?

V

vbMark

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!
 
T

Telmo Costa

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
 
G

Guest

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
 
J

Jon Skeet [C# MVP]

vbMark said:
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.
 
V

vbMark

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?
 
V

vbMark

vbMark said:
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();
 
J

Jon Skeet [C# MVP]

vbMark said:
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);
}
}
}
}
}
 
V

vbMark

vbMark said:
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.
 
J

Jon Skeet [C# MVP]

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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top