Page 1
Page 2
If MyString is a Unicode string and
that's what you want to write, then all is well. But what
if you'd rather same some space and write it as ANSI? In
that case I have a simple shortcut. Since we know that an
ANSI capable language represented as Unicode has a zero
for every even byte, we can just step through the string,
grabbing every odd byte and appending it to another string
like this:
Private Function UnicodeToAnsi(WideString
As String) As String
Dim i As Integer
For i = 1 To LenB(WideString) Step 2
UnicodeToAnsi = UnicodeToAnsi & MidB(WideString, i,
1)
Next
End Function
Another handy part of API file writing
is that we can actually begin writing at any point in the
file. This allows you to not only append to the end of a
file, but also to seek to a specific position in the middle
of the file and begin writing there (making fixed-width
field handling cake).
This is done by setting the file position
pointer with the SetFilePointer API. In our sample application,
we append by moving to the end of the file and offsetting
zero like this:
SetFilePointer hFile, 0, 0, FILE_END
For more information of the options
for both WriteFile and SetFilePointer, take a look in the
eMbedded Visual Tools Help.
Writing binary data is really no different
than writing a string. As an example, let's say we have
binary data representing "BINARY STRING" in a
byte array and we want to write it. The following code from
the sample application shows how to do that:
Dim data As Byte
Dim i As Integer
Dim outbuffer As String
' binary data (in hex) will be: 42494E4152592044415441
data = Array(&H42, &H49, &H4E, &H41, &H52,
&H59, &H20, &H44, &H41, &H54, &H41)
' turn binary data into a string
For i = 0 To UBound(data) - 1
outbuffer = outbuffer & ChrB(data(i))
Next
WriteString outbuffer
Reading From A File
Once you've got data into a file, the
only thing left to do is get it back out. This is done,
not surprisingly, using the ReadFile API. Again, before
reading you have to get a file handle using CreateFile.
Before reading a file, though, we need
to allocate enough space in our input buffer to hold that
data. The easiest way to do this is to first call the GetFileSize
API to get the byte length of the file like this:
FileSize = GetFileSize(hFile, 0)
Then we create an input buffer:
Contents = String(FileSize, 0)
And finally we read into that buffer:
ReadFile hFile, Contents, FileSize, dwRead,
0
The only item that might be remaining
is displaying the file contents. For a string that's simple;
just set a TextBox's Text property to the Contents and you're
done. For binary data, most of the data will display as
unprintable characters (little squares) so showing the data
in hexadecimal format is nice. We can convert our data quickly
to space delimited hex format with the following code:
For i = 1 To FileSize
output = output & Right("0" & Hex(AscB(MidB(Contents,
i, 1))) & " ", 3)
Next i
That's all there is to it. This provides
you with a far more robust set of file handling methods
than the File control itself and these methods can also
be very useful when using the COM ports (since they work
just like a file!). Happy developing.
Previous Page