Main Topics
Browse All Topics
I need to concatenate two binary files. but i need to do this operation as fast as it can be. Can you show me a way to do this ?
thanks
(I am using windows and VS 2008-native c++)
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
You could use the code here:
Appending One File to Another File
http://msdn.microsoft.com/
First of all, it depends on what you understand by 'concatenate'. Do you want to create a third file that contains the concatenation of the two other files ? Or do you want to append the second file to the end of the first file ?
In any case, an easy way to copy binary data from one file to another (assuming we're talking about big files), is to have a fixed size buffer in memory, and use 'read' to fill the buffer from one file, and then 'write' to write the buffer to the other file. Repeat this in a loop until the entire file has been copied.
http://cplusplus.com/refer
http://cplusplus.com/refer
To open a file stream for appending, you can use the 'app' flag :
http://cplusplus.com/refer
It could be as simple as :
I suggest the combination of Infinity08 http:#25301349 and itkamaraj http:#25300124. Using the read file .rdbuf() method saves you from allocating buffer and explicit looping. You probably do not want to add a space between the files. Also, flushing is done when the file is closed.
If you do not want to append one existing file to the other but rather create the third file as concatenation of the two, then you have to do something like (no checking here):
std::ofstream fdest("fout.bin", std::ios_base::out | std::ios_base::binary);
std::ifstream fin1("file1.bin", std::ios_base::in | std::ios_base::binary);
std::ifstream fin2("file2.bin", std::ios_base::in | std::ios_base::binary);
fdest << fin1.rdbuf();
fdest << fin2.rdbuf();
fdest.close();
fin1.close();
fin2.close();
maybe the answer should contain WinAPI like:
HANDLE hFile1 = CreateFile(,GENERIC_WRITE,
SeekFile
HANDLE hFile2 = CreateFile(,GENERIC_READ,)
ReadFile(
WriteFile(hFile1);
CloseHan
CloseHan
The question is about Windows.
And this WinAPI way looks for me much better then ofstream appendFile. :)
Well kuroji, this was not a HOMEWORK question. The windows service that i am writing is supposed to concatenate thousands of binary file pairs. And the performance is extremely important.
And i did not have enough time to try different ways to do this operation.
The file sizes are small (20 MB max) but there are thousands of them.
Thanks all for your answers.
My apologies parabellum.
The only additional suggestion I can add to improve performance is to find out the disk sector size and use a file buffer size that is an integer multiple of that value.
DeviceIoControl IOCTL_DISK_GET_DRIVE_GEOME
typedef struct _DISK_GEOMETRY_EX {
DISK_GEOMETRY Geometry;
LARGE_INTEGER DiskSize;
BYTE Data[1];
}
typedef struct _DISK_GEOMETRY {
LARGE_INTEGER Cylinders;
MEDIA_TYPE MediaType;
DWORD TracksPerCylinder;
DWORD SectorsPerTrack;
DWORD BytesPerSector;
}
The only u
Business Accounts
Answer for Membership
by: itkamarajPosted on 2009-09-10 at 06:46:27ID: 25300118
like this ?
Select allOpen in new window