I want to search example simple file binary read,write... in the form record, can i help you ? thanks
Printable View
I want to search example simple file binary read,write... in the form record, can i help you ? thanks
What do you mean "in the form record"?
Binary access is like
You can Get any number of bytes.Code:Open FileName for Binary As #FNum
Get #FNum, ChunkSize, BufferVar
'or
Put #FNum, BufferVar 'if writing.
Close #FNum
If you leave away the ChunkSize, the amount of bytes read depends on the data type of the BufferVar-variable you are using. If you specify the ChunkSize, the BufferVar must be able to hold the resulting data.
The mean is my many records and one record have got fields:
Fullname
Address
PhoneNumber
and i want to write records into file FileReord.dat, what do you understand ?
I think you are referring to random access mode
Open FileName For Random As #FNum Len=RecordLen
You need to know the length of your record, then you can use the Get statement to read records from the file. You have to provide a buffer for one record like that:
Dim RecBuf As String
RecBuf = Space$(RecordLen)
Then you use Get:
Get #Fnum, RecordNumber, RecBuf
If you omit RecordNumber, always the next record is read with consecutive Get statements.
After you have read one record you have to know the field lengths of your data fields to separte them from the record buffer.
I think it is also possible to declare the record as a UDT and read consecutive records in that UDT which is already divided in the field sections
and in a sub you could read all records likeCode:Private Type MyRecordType
FullName As String * 25
Address As String *40
PhoneNumber as String * 20
End Type
Private CurRecord as MyRecordType
...
Instead of printing you might wish to do something useful with the record's data.Code:Dim CurRecord As MyRecordType
Open FileName For Random As #1 Len = Len(CurRecord)
While Not EOF(1)
Get #1, , CurRecord
Print "Name: "; CurRecord.FullName
Print "Address: "; CurRecord.Address
Print "Phone: "; CurRecord.PhoneNumber
Wend
Close #1
Note that I only used made-up field lengths in the UDT. 24, 40 and 20 might not be the lengths of your data fields.
You need to know the field lengths of your record fields and use them in the definition of the Record type instead.
I forgot: To write records you need the same declaration of the record type.
This would write a new record to the file. After Open, it would start writing at the first record.Code:Dim Rec As MyRecordType
Open FileName For Random As #1 Len = Len(CurRecord)
Rec.Name = "wahwah"
Rec.Address = "Somewhere"
Rec.PhoneNumber = "1234"
Put #1, , Rec
Close #1
Put allows you always to specify the recordnumber, so as you can overwrite any record even in the middle of a file.
And there is the Seek statement to put your current record pointer to a specific record.