OK. Her er en lille demo.
Pascal kode:
program wrtrec;
type
testrec = packed record
a : SmallInt;
b : LongInt;
c : string[5];
d : array [1..2] of string[10];
end;
var
testfile : file of testrec;
buf : testrec;
begin
assign(testfile, 'C:\work\test.dat');
rewrite(testfile);
buf.a := 123;
buf.b := 456;
buf.c := 'ABC';
buf.d[1] := 'DEF';
buf.d[2] := 'GEH';
write(testfile, buf);
buf.a := buf.a + 1;
buf.b := buf.b + 1;
write(testfile, buf);
close(testfile);
readln;
end.
VBS kode:
Function ReadSmallInt(binf) ' only works with positive numbers
buf = binf.Read(2)
ReadSmallInt = 256 * AscB(MidB(buf, 2, 1)) + AscB(MidB(buf, 1, 1))
End Function
Function ReadLongInt(binf) ' only works with positive numbers
buf = binf.Read(4)
ReadLongInt = 256 * 256 * 256 * AscB(MidB(buf, 4, 1)) + 256 * 256 * AscB(MidB(buf, 3, 1)) + 256 * AscB(MidB(buf, 2, 1)) + AscB(MidB(buf, 1, 1))
End Function
Function BytesToString(buf, start, len)
s = ""
For i = 1 To len
s = s & Chr(AscB(MidB(buf, start + i - 1, 1)))
Next
BytesToString = S
End Function
Function ReadString(binf, len)
buf = binf.Read(1 + len)
ReadString = BytesToString(buf, 2, AscB(MidB(buf, 1, 1)))
End Function
Set binf = CreateObject("ADODB.Stream")
binf.Type = 1 ' adTypeBinary
binf.Open
binf.LoadFromFile "test.dat"
Do While Not binf.EOS
a = ReadSmallInt(binf)
b = ReadLongInt(binf)
c = ReadString(binf, 5)
d1 = ReadString(binf, 10)
d2 = ReadString(binf, 10)
MsgBox CStr(a) & "," & CStr(b) & "," & c & "," & d1 & "," & d2
Loop
binf.Close
Det virker hos mig.