open, read, write file binary ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: open, read, write file binary ?

  1. #1
    Join Date
    Sep 2007
    Posts
    198

    open, read, write file binary ?

    I want to search example simple file binary read,write... in the form record, can i help you ? thanks

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: open, read, write file binary ?

    What do you mean "in the form record"?
    Binary access is like
    Code:
    Open FileName for Binary As #FNum
    Get #FNum, ChunkSize, BufferVar
    'or
    Put #FNum, BufferVar 'if writing.
    Close #FNum
    You can Get any number of bytes.
    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.

  3. #3
    Join Date
    Sep 2007
    Posts
    198

    Re: open, read, write file binary ?

    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 ?

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: open, read, write file binary ?

    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
    Code:
    Private Type MyRecordType
      FullName As String * 25
      Address As String *40
      PhoneNumber as String * 20
    End Type
    
    Private CurRecord as MyRecordType
    and in a sub you could read all records like
    ...
    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
    Instead of printing you might wish to do something useful with the record's data.
    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.

  5. #5
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: open, read, write file binary ?

    I forgot: To write records you need the same declaration of the record type.
    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
    This would write a new record to the file. After Open, it would start writing at the first record.
    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.
    Last edited by WoF; January 27th, 2010 at 08:41 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center