Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please Help - Combining Binary Files

Status
Not open for further replies.

amutinu

Programmer
Oct 19, 2001
50
0
0
US
Hello All,
I am creating a VB App that will merge/combine two binary files into one large file. I am using the Open command to open the file in binary read mode and the output file in binary write mode. But somehow the two files are not merging. I only see the 2nd file in the document. So when i use the Get and Put commands to create a new file, it only displays the 2nd file instead of both files (One appended after the other).

Can anyone please shed some light on how to combine two binary files in VB? I would really appreciate your help.

thanks,

My Code:

FileNum2 = FreeFile
Open bfilename3 For Binary Access Write As #FileNum2

For RecordNumber = 1 To 2
FileNum1 = FreeFile
Open bfilename For Binary Access Read As #FileNum1

fl = FileLen(bfilename) - 1
ReDim binbyte(fl)

Get #FileNum1, , binbyte
Close #FileNum1

Put #FileNum2, , binbyte

Next
 
You Open bfilename twice. You have two copies of the same file. Forget the For/Next
Dim binbyte() as byte
FileNum2 = FreeFile
Open bfilename3 For Binary Access Write As #FileNum2

FileNum1 = FreeFile
Open bfilename1 For Binary Access Read As #FileNum1

fl = FileLen(bfilename) - 1
ReDim binbyte(fl)

Get #FileNum1, , binbyte
Close #FileNum1

Put #FileNum2, , binbyte

FileNum1 = FreeFile
Open bfilename2 For Binary Access Read As #FileNum1
fl = FileLen(bfilename2) - 1
ReDim binbyte(fl)

Get #FileNum1, , binbyte
Close #FileNum1
Close #FileNum2

Compare Code (Text)
Generate Sort in VB or VBScript
 
Hi JohnYingling
thanks for your response but the code that you have mentioned does not combine/merge two binary files. All it does is creates a new file with the first binary file's content. I am still looking for a way to combine/merge two binary files into one file. i tried puting another "put" command right before closing the #filenum2 but that didn't work. All it did was replace the content with the 2nd file instead of merging.

Also when i look at the size of the new file, it matches the combined size of file1 and file2. It's just that the content is from file1 only.

Any thoughts?

thanks,
 
Heres a caveman method that might help you find the bug:

dim FpA%
dim FpB%
dim FpMerg%
dim Temp as byte
FpA=FreeFile()
FpB=FreeFile()
FpMerg=FreeFile()

Open FileNameA for binary as FpA
Open FileNameB for binary as FpB
Open FileNameMerg for binary as FpMerg

do until EOF(FpA)
get #FpA,,Temp
put #FpMerg,,Temp
loop

do until EOF(FpB)
get #FpB,,Temp
put #FpMerg,,Temp
loop

Close(FpA)
Close(FpB)
Close(FpMerg)

 
All of those should work. My contribution to confuse or clarify the issue:

Merge two files into a third file...
[tt]
ff = FreeFile
Open FirstFile$ For Binary As #ff
G1$ = String$(LOF(ff), 0)
Get #ff, 1, G1$
Close #ff
ff = FreeFile
Open SecondFile$ For Binary As #ff
G2$ = String$(LOF(ff), 0)
Get #ff, 1, G2$
Close #ff
If Dir(ThirdFile$) <> &quot;&quot; Then Kill ThirdFile$
ff = FreeFile
Open ThirdFile$ For Binary As #ff
Put #ff, 1, G1$
Put #ff, , G2$
Close #ff
[/tt]
Merge two files by replacing the first file...
[tt]
f1 = FreeFile
Open FirstFile$ For Binary As #f1
f2 = FreeFile
Open SecondFile$ For Binary As #f2
G2$ = String$(LOF(f2), 0)
Get #f2, 1, G2$
Put #f1, LOF(f1)+1, G2$
Close
[/tt]
VCA.gif
 
chiuchimu & Alt255,
thanks guys for your responses but i was wondering if this would work for adobe pdf files. the binary files i have are pdf files and somehow when i use your code, it doesn't combine the files. It just creates the third file with the content of the second file.

Any ideas/thoughts as to why this might be happening?

thanks and i appreciate your responses,
 
Most files have whats called a header file. This is the first section of the file and it contains data on interpeting the data. I have never manipulated a pdf file my self but I bet that is why you cant simply merge the files. I have experience in dealing with bmp and pcx bitmap files and to merge these files requires not only manipulation of the header file but also manipulation of the raw data it self. I suggest finding out about the files format first before trying any further.

Here is an example of a bmp header to give you an Idea of what I'm talking about:Bmp header

type BmpHeader
ID as integer
FileSize as long
Reserved(2) As integer
ImageOffSet as Long
HeaderfileSize as integer
NotUsed as integer
Xresolution as long
Yresolution as long
NumberOfPlanes As int
BitsPerPixel as int
CompressionMethod as long
BitMapSize as long
HorizantalResolution as long
VerticalResolution as long
NumberOfColors as long
NumberSignificantColors as long
end type

There is a value called bitmapsize. A program reading this value would ignore anything beyond this point;simular to what is happening to you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top