rnshaw
asked on
File I/O question for "nigelrowe"
This question is specifically for nigelrowe.
You told me how to read a file to a byte array, but how to I write it back to a file then?
I figured I should give you points for answering the second question I had.
Thanks
Ryan
You told me how to read a file to a byte array, but how to I write it back to a file then?
I figured I should give you points for answering the second question I had.
Thanks
Ryan
ASKER
this was the original example:
<SNIP>
Dim byteArray As Variant
Dim fhandle As Integer
fhandle = FreeFile
Open fname For Binary Access Read As fhandle
byteArray = InputB(LOF(fhandle), fhandle)
Close fhandle
</SNIP>
this is what Im doing now...
<SNIP>
Dim byteArray As Variant
Dim fhandle As Integer
fhandle = FreeFile
Open "C:\Test2.doc" For Binary Access Read As fhandle
byteArray = InputB(LOF(fhandle), fhandle)
Close fhandle
fhandle = FreeFile
Open "C:\BTest.doc" For Binary As fhandle
Put #fhandle, , byteArray
Close fhandle
</SNIP>
the above is just to test the theory, when I open BTest.doc, its all garbage...
Ryan
<SNIP>
Dim byteArray As Variant
Dim fhandle As Integer
fhandle = FreeFile
Open fname For Binary Access Read As fhandle
byteArray = InputB(LOF(fhandle), fhandle)
Close fhandle
</SNIP>
this is what Im doing now...
<SNIP>
Dim byteArray As Variant
Dim fhandle As Integer
fhandle = FreeFile
Open "C:\Test2.doc" For Binary Access Read As fhandle
byteArray = InputB(LOF(fhandle), fhandle)
Close fhandle
fhandle = FreeFile
Open "C:\BTest.doc" For Binary As fhandle
Put #fhandle, , byteArray
Close fhandle
</SNIP>
the above is just to test the theory, when I open BTest.doc, its all garbage...
Ryan
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
get #fhandle,,byteArray
doesn't work, I get this:
Error #458
Variable uses an Automation type not supported in Visual Basic.
doesn't work, I get this:
Error #458
Variable uses an Automation type not supported in Visual Basic.
ASKER
thats because I didn't change the datatype of bytearray to Byte...
but it doesnt work either, it only puts one char on the doc...
any other suggestions
but it doesnt work either, it only puts one char on the doc...
any other suggestions
That's OK because we didn't populate that variable byteArray with nothing else but default value!
I don't know what you store in that variable, either ;)
I don't know what you store in that variable, either ;)
ASKER
ahh, I figured it out
read all day on MSDN found this....
Dim fl As Long
Dim FileNum As Long
Dim binbyte() As Byte
Dim binfilestr As String
FileNum = FreeFile
Open "C:\Test2.doc" For Binary Access Read As #FileNum
fl = FileLen("C:\Test2.doc")
ReDim binbyte(fl)
Get #FileNum, , binbyte
Close #FileNum
Open "C:\BBTest.doc" For Binary As #FileNum
Put #FileNum, , binbyte
Close #FileNum
this will help greatly my other question that others where helping me on
Thanks, I'll see about splitting up the points between you guys for trying to help me.
read all day on MSDN found this....
Dim fl As Long
Dim FileNum As Long
Dim binbyte() As Byte
Dim binfilestr As String
FileNum = FreeFile
Open "C:\Test2.doc" For Binary Access Read As #FileNum
fl = FileLen("C:\Test2.doc")
ReDim binbyte(fl)
Get #FileNum, , binbyte
Close #FileNum
Open "C:\BBTest.doc" For Binary As #FileNum
Put #FileNum, , binbyte
Close #FileNum
this will help greatly my other question that others where helping me on
Thanks, I'll see about splitting up the points between you guys for trying to help me.
mshaw, I think that should be...
open "file" for binary as #1
put #1,bByteArray
close #1
This may also help from MSDN...
For files opened in Random mode, the following rules apply:
If the length of the data being written is less than the length specified in the Len clause of the Open method, Put writes subsequent records on record-length boundaries. The space between the end of one record and the beginning of the next record is padded with the existing contents of the file buffer. Because the amount of padding data cannot be determined with any certainty, it generally is a good idea to have the record length match the length of the data being written. If the length of the data being written is greater than the length specified in the Len clause of the Open method, an error occurs.
If the variable being written is a Variant of a numeric type, Put writes 2 bytes identifying the VarType of the Variant and then writes the variable. For example, when writing a Variant of VarType 3, Put writes 6 bytes: 2 bytes identifying the Variant as VarType 3 (Long) and 4 bytes containing the Long data. The record length specified by the Len clause in the Open method must be at least 2 bytes greater than the actual number of bytes required to store the variable.
You can use the Put method to write a Variant array to disk, but you cannot use Put to write a scalar Variant containing an array to disk. You also cannot use Put to write objects to disk.
If the variable being written is a Variant of VarType 8 (String), Put writes 2 bytes identifying the VarType and 2 bytes indicating the length of the string. It then writes the string data. The record length specified by the Len clause in the Open method must be at least 4 bytes greater than the actual length of the string.
If the variable being written is a dynamic array, Put writes a descriptor whose length equals 2 plus 8 times the number of dimensions, that is, 2 + 8 * NumberOfDimensions. The record length specified by the Len clause in the Open method must be greater than or equal to the sum of all the bytes required to write the array data and the array descriptor. For example, the following array declaration requires 118 bytes when the array is written to disk.
open "file" for binary as #1
put #1,bByteArray
close #1
This may also help from MSDN...
For files opened in Random mode, the following rules apply:
If the length of the data being written is less than the length specified in the Len clause of the Open method, Put writes subsequent records on record-length boundaries. The space between the end of one record and the beginning of the next record is padded with the existing contents of the file buffer. Because the amount of padding data cannot be determined with any certainty, it generally is a good idea to have the record length match the length of the data being written. If the length of the data being written is greater than the length specified in the Len clause of the Open method, an error occurs.
If the variable being written is a Variant of a numeric type, Put writes 2 bytes identifying the VarType of the Variant and then writes the variable. For example, when writing a Variant of VarType 3, Put writes 6 bytes: 2 bytes identifying the Variant as VarType 3 (Long) and 4 bytes containing the Long data. The record length specified by the Len clause in the Open method must be at least 2 bytes greater than the actual number of bytes required to store the variable.
You can use the Put method to write a Variant array to disk, but you cannot use Put to write a scalar Variant containing an array to disk. You also cannot use Put to write objects to disk.
If the variable being written is a Variant of VarType 8 (String), Put writes 2 bytes identifying the VarType and 2 bytes indicating the length of the string. It then writes the string data. The record length specified by the Len clause in the Open method must be at least 4 bytes greater than the actual length of the string.
If the variable being written is a dynamic array, Put writes a descriptor whose length equals 2 plus 8 times the number of dimensions, that is, 2 + 8 * NumberOfDimensions. The record length specified by the Len clause in the Open method must be greater than or equal to the sum of all the bytes required to write the array data and the array descriptor. For example, the following array declaration requires 118 bytes when the array is written to disk.
Oops. No, forget that. Its for a File type object.
This is more appropriate....
Visual Basic for Applications Reference
Put Statement
Writes data from a variable to a disk file.
Syntax
Put [#]filenumber, [recnumber], varname
The Put statement syntax has these parts:
Part Description
filenumber Required. Any valid file number.
recnumber Optional. Variant (Long). Record number (Random mode files) or byte number (Binary mode files) at which writing begins.
varname Required. Name of variable containing data to be written to disk.
Remarks
Data written with Put is usually read from a file with Get.
The first record or byte in a file is at position 1, the second record or byte is at position 2, and so on. If you omit recnumber, the next record or byte after the last Get or Put statement or pointed to by the last Seek function is written. You must include delimiting commas, for example:
Put #4,,FileBuffer
For files opened in Random mode, the following rules apply:
If the length of the data being written is less than the length specified in the Len clause of the Open statement, Put writes subsequent records on record-length boundaries. The space between the end of one record and the beginning of the next record is padded with the existing contents of the file buffer. Because the amount of padding data can't be determined with any certainty, it is generally a good idea to have the record length match the length of the data being written. If the length of the data being written is greater than the length specified in the Len clause of the Open statement, an error occurs.
If the variable being written is a variable-length string, Put writes a 2-byte descriptor containing the string length and then the variable. The record length specified by the Len clause in the Open statement must be at least 2 bytes greater than the actual length of the string.
If the variable being written is a Variant of a numeric type, Put writes 2 bytes identifying the VarType of the Variant and then writes the variable. For example, when writing a Variant of VarType 3, Put writes 6 bytes: 2 bytes identifying the Variant as VarType 3 (Long) and 4 bytes containing the Long data. The record length specified by the Len clause in the Open statement must be at least 2 bytes greater than the actual number of bytes required to store the variable.
Note You can use the Put statement to write a Variant array to disk, but you can't use Put to write a scalar Variant containing an array to disk. You also can't use Put to write objects to disk.
If the variable being written is a Variant of VarType 8 (String), Put writes 2 bytes identifying the VarType, 2 bytes indicating the length of the string, and then writes the string data. The record length specified by the Len clause in the Open statement must be at least 4 bytes greater than the actual length of the string.
If the variable being written is a dynamic array, Put writes a descriptor whose length equals 2 plus 8 times the number of dimensions, that is, 2 + 8 * NumberOfDimensions. The record length specified by the Len clause in the Open statement must be greater than or equal to the sum of all the bytes required to write the array data and the array descriptor. For example, the following array declaration requires 118 bytes when the array is written to disk.
Dim MyArray(1 To 5,1 To 10) As Integer
The 118 bytes are distributed as follows: 18 bytes for the descriptor (2 + 8 * 2), and 100 bytes for the data (5 * 10 * 2).
If the variable being written is a fixed-size array, Put writes only the data. No descriptor is written to disk.
If the variable being written is any other type of variable (not a variable-length string or a Variant), Put writes only the variable data. The record length specified by the Len clause in the Open statement must be greater than or equal to the length of the data being written.
Put writes elements of user-defined types as if each were written individually, except there is no padding between elements. On disk, a dynamic array in a user-defined type written with Put is prefixed by a descriptor whose length equals 2 plus 8 times the number of dimensions, that is, 2 + 8 * NumberOfDimensions. The record length specified by the Len clause in the Open statement must be greater than or equal to the sum of all the bytes required to write the individual elements, including any arrays and their descriptors.
For files opened in Binary mode, all of the Random rules apply, except:
The Len clause in the Open statement has no effect. Put writes all variables to disk contiguously; that is, with no padding between records.
For any array other than an array in a user-defined type, Put writes only the data. No descriptor is written.
Put writes variable-length strings that are not elements of user-defined types without the 2-byte length descriptor. The number of bytes written equals the number of characters in the string. For example, the following statements write 10 bytes to file number 1:
VarString$ = String$(10," ")
Put #1,,VarString$
Visual Basic for Applications Reference
Put Statement
Writes data from a variable to a disk file.
Syntax
Put [#]filenumber, [recnumber], varname
The Put statement syntax has these parts:
Part Description
filenumber Required. Any valid file number.
recnumber Optional. Variant (Long). Record number (Random mode files) or byte number (Binary mode files) at which writing begins.
varname Required. Name of variable containing data to be written to disk.
Remarks
Data written with Put is usually read from a file with Get.
The first record or byte in a file is at position 1, the second record or byte is at position 2, and so on. If you omit recnumber, the next record or byte after the last Get or Put statement or pointed to by the last Seek function is written. You must include delimiting commas, for example:
Put #4,,FileBuffer
For files opened in Random mode, the following rules apply:
If the length of the data being written is less than the length specified in the Len clause of the Open statement, Put writes subsequent records on record-length boundaries. The space between the end of one record and the beginning of the next record is padded with the existing contents of the file buffer. Because the amount of padding data can't be determined with any certainty, it is generally a good idea to have the record length match the length of the data being written. If the length of the data being written is greater than the length specified in the Len clause of the Open statement, an error occurs.
If the variable being written is a variable-length string, Put writes a 2-byte descriptor containing the string length and then the variable. The record length specified by the Len clause in the Open statement must be at least 2 bytes greater than the actual length of the string.
If the variable being written is a Variant of a numeric type, Put writes 2 bytes identifying the VarType of the Variant and then writes the variable. For example, when writing a Variant of VarType 3, Put writes 6 bytes: 2 bytes identifying the Variant as VarType 3 (Long) and 4 bytes containing the Long data. The record length specified by the Len clause in the Open statement must be at least 2 bytes greater than the actual number of bytes required to store the variable.
Note You can use the Put statement to write a Variant array to disk, but you can't use Put to write a scalar Variant containing an array to disk. You also can't use Put to write objects to disk.
If the variable being written is a Variant of VarType 8 (String), Put writes 2 bytes identifying the VarType, 2 bytes indicating the length of the string, and then writes the string data. The record length specified by the Len clause in the Open statement must be at least 4 bytes greater than the actual length of the string.
If the variable being written is a dynamic array, Put writes a descriptor whose length equals 2 plus 8 times the number of dimensions, that is, 2 + 8 * NumberOfDimensions. The record length specified by the Len clause in the Open statement must be greater than or equal to the sum of all the bytes required to write the array data and the array descriptor. For example, the following array declaration requires 118 bytes when the array is written to disk.
Dim MyArray(1 To 5,1 To 10) As Integer
The 118 bytes are distributed as follows: 18 bytes for the descriptor (2 + 8 * 2), and 100 bytes for the data (5 * 10 * 2).
If the variable being written is a fixed-size array, Put writes only the data. No descriptor is written to disk.
If the variable being written is any other type of variable (not a variable-length string or a Variant), Put writes only the variable data. The record length specified by the Len clause in the Open statement must be greater than or equal to the length of the data being written.
Put writes elements of user-defined types as if each were written individually, except there is no padding between elements. On disk, a dynamic array in a user-defined type written with Put is prefixed by a descriptor whose length equals 2 plus 8 times the number of dimensions, that is, 2 + 8 * NumberOfDimensions. The record length specified by the Len clause in the Open statement must be greater than or equal to the sum of all the bytes required to write the individual elements, including any arrays and their descriptors.
For files opened in Binary mode, all of the Random rules apply, except:
The Len clause in the Open statement has no effect. Put writes all variables to disk contiguously; that is, with no padding between records.
For any array other than an array in a user-defined type, Put writes only the data. No descriptor is written.
Put writes variable-length strings that are not elements of user-defined types without the 2-byte length descriptor. The number of bytes written equals the number of characters in the string. For example, the following statements write 10 bytes to file number 1:
VarString$ = String$(10," ")
Put #1,,VarString$
ASKER
I read MSDN and figured it out yesterday at about 4:30pm
this is what I did CLient Side:
<SNIP>
Private Sub Command1_Click()
Dim oTest As wtTest.cTest
Dim fl As Long
Dim FileNum As Long
Dim binbyte() As Byte
FileNum = FreeFile
Open "C:\Test2.doc" For Binary Access Read As #FileNum
fl = FileLen("C:\Test2.doc")
ReDim binbyte(fl)
Get #FileNum, , binbyte
Close #FileNum
Set oTest = New wtTest.cTest
oTest.FaxMe "7632773709", binbyte
Set oTest = Nothing
End Sub
</SNIP>
this is what I did server side
<SNIP>
Public Sub FaxMe(FaxToNumber As String, bDocContentByteArray() As Byte)
Dim oFaxServer As RFCOMAPILib.FaxServer
Dim oFax As RFCOMAPILib.Fax
Dim x As Long
Set oFaxServer = New RFCOMAPILib.FaxServer
x = FreeFile()
oFaxServer.AuthorizationUs erID = "RyanS"
oFaxServer.ServerName = "PonyExpress"
Set oFax = oFaxServer.CreateObject(co Fax)
Open "C:\FaxTester.doc" For Binary As x
Put #x, , bDocContentByteArray
Close #x
oFax.Attachments.Add "C:\FaxTester.doc"
oFax.FromName = "RyanClient"
oFax.ToName = "RyanTest"
oFax.ToFaxNumber = FaxToNumber
oFax.Send
Set oFax = Nothing
Set oFaxServer = Nothing
Kill "C:\FaxTester.doc"
End Sub
</SNIP>
had to make sure that the Binary Byte Array was really an array....
still gonna award points
gonna split the points for all the help I got.
thanks, everything you guys suggested helped me to find the answer myself.
Ryan
this is what I did CLient Side:
<SNIP>
Private Sub Command1_Click()
Dim oTest As wtTest.cTest
Dim fl As Long
Dim FileNum As Long
Dim binbyte() As Byte
FileNum = FreeFile
Open "C:\Test2.doc" For Binary Access Read As #FileNum
fl = FileLen("C:\Test2.doc")
ReDim binbyte(fl)
Get #FileNum, , binbyte
Close #FileNum
Set oTest = New wtTest.cTest
oTest.FaxMe "7632773709", binbyte
Set oTest = Nothing
End Sub
</SNIP>
this is what I did server side
<SNIP>
Public Sub FaxMe(FaxToNumber As String, bDocContentByteArray() As Byte)
Dim oFaxServer As RFCOMAPILib.FaxServer
Dim oFax As RFCOMAPILib.Fax
Dim x As Long
Set oFaxServer = New RFCOMAPILib.FaxServer
x = FreeFile()
oFaxServer.AuthorizationUs
oFaxServer.ServerName = "PonyExpress"
Set oFax = oFaxServer.CreateObject(co
Open "C:\FaxTester.doc" For Binary As x
Put #x, , bDocContentByteArray
Close #x
oFax.Attachments.Add "C:\FaxTester.doc"
oFax.FromName = "RyanClient"
oFax.ToName = "RyanTest"
oFax.ToFaxNumber = FaxToNumber
oFax.Send
Set oFax = Nothing
Set oFaxServer = Nothing
Kill "C:\FaxTester.doc"
End Sub
</SNIP>
had to make sure that the Binary Byte Array was really an array....
still gonna award points
gonna split the points for all the help I got.
thanks, everything you guys suggested helped me to find the answer myself.
Ryan
Reducing points for split.
Lunchy
Community Support Moderator
Lunchy
Community Support Moderator
rnshaw, please post another 100 point question in this topic area for nigelrowe. Please comment here to notify nigelrowe when the question has been posted.
Lunchy
Community Support Moderator
Lunchy
Community Support Moderator
ASKER
nigelrowe
go get your points by answering in this question
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=visualbasic&qid=20133445
go get your points by answering in this question
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=visualbasic&qid=20133445
put #1,,bByteArray
close #1