Solved

File I/O question for  "nigelrowe"

Posted on 2001-06-05
14
199 Views
Last Modified: 2012-05-04
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
0
Comment
Question by:rnshaw
  • 6
  • 3
  • 2
  • +2
14 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 6156844
open "file" for binary as #1
put #1,,bByteArray
close #1
0
 

Author Comment

by:rnshaw
ID: 6156992
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
0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 100 total points
ID: 6157023
may be this:
Dim byteArray As byte
Dim fhandle As Integer

  fhandle = FreeFile
  Open "C:\Test2.doc" For Binary Access Read As #fhandle
  get #fhandle,,byteArray
  Close #fhandle
 
 
0
 

Author Comment

by:rnshaw
ID: 6157163
get #fhandle,,byteArray

doesn't work, I get this:

Error #458
Variable uses an Automation type not supported in Visual Basic.


0
 

Author Comment

by:rnshaw
ID: 6157177
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
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6157571
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 ;)
0
 

Author Comment

by:rnshaw
ID: 6157795
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Expert Comment

by:nigelrowe
ID: 6158737
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.

0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6158762
Oops. No, forget that. Its for a File type object.
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6158770
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$

0
 

Author Comment

by:rnshaw
ID: 6159599
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.AuthorizationUserID = "RyanS"
    oFaxServer.ServerName = "PonyExpress"
   
    Set oFax = oFaxServer.CreateObject(coFax)
   
    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
0
 
LVL 2

Expert Comment

by:Lunchy
ID: 6179005
Reducing points for split.

Lunchy
Community Support Moderator
0
 
LVL 2

Expert Comment

by:Lunchy
ID: 6179013
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
0
 

Author Comment

by:rnshaw
ID: 6179051
nigelrowe
go get your points by answering in this question


http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=visualbasic&qid=20133445
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now