Read and Write Binary Files in VB6, VBScript, ASP
The Chilkat FileAccess ActiveX is a freeware component that may be used to read and write binary files in VBScript, ASP, VB6, etc. Here are some sample code fragments:
VBScript
' Download the Chilkat FileAccess ActiveX (freeware) from here: ' http://www.chilkatsoft.com/download/FileAccess.zip set fac = CreateObject("Chilkat.FileAccess") ' Read an entire file as binary data (returns Variant containing byte array) byteData = fac.ReadEntireFile("dude.gif") ' Save bytes to a file: success = fac.WriteEntireFile("out.gif", byteData) if (success = 0) then MsgBox "Failed to write file." end if
ASP
<% ' Download the Chilkat FileAccess ActiveX (freeware) from here: ' http://www.chilkatsoft.com/download/FileAccess.zip set fac = Server.CreateObject("Chilkat.FileAccess") ' Read the entire binary file into a Variant containing a byte array fileData = fac.ReadEntireFile(Server.MapPath("data/dude.gif")) ' Write the entire byte array to a file: success = fac.WriteEntireFile(Server.MapPath("data/out.gif"), fileData) if (success = 0) then Response.Write "Failed." else Response.Write "Success." end if %>
Visual Basic 6.0
' Download the Chilkat FileAccess ActiveX (freeware) from here: ' http://www.chilkatsoft.com/download/FileAccess.zip ' Add a reference to Chilkat FileAccess Dim fac As New CkFileAccess Dim fileData() As Byte fileData = fac.ReadEntireFile("dude.gif") Dim success As Long success = fac.WriteEntireFile("out.gif", fileData) If (success = 0) Then MsgBox "Failed to write file." End If
Here are some example for reading/writing binary files in C#, VB.NET, and C/C++:
C/C++
FILE *fp = fopen("dude.gif","rb"); // How big is the file? fseek(fp, 0, SEEK_END); long size = ftell(fp); unsigned char *fileData = new unsigned char[size]; // Position to the start of the file: fseek(fp,0,SEEK_SET); fread(fileData,1,size,fp); fclose(fp); fp = fopen("out.gif","wb"); fwrite(fileData,1,size,fp); fclose(fp);
C#
byte[] fileData = System.IO.File.ReadAllBytes("dude.gif"); System.IO.File.WriteAllBytes("out.gif", fileData);
VB.NET
Dim fileData As Byte() fileData = System.IO.File.ReadAllBytes("dude.gif") System.IO.File.WriteAllBytes("out.gif", fileData)
admin
0
Tags :