Results 1 to 4 of 4

Thread: Binary Saving Question...

  1. #1
    Guest
    I have a string variable that is set from a textbox text string and I need to save it to a binary file. There are also a bunch of other misc values I have to save to this same file too (Boolean, Single, and String values)

    Dim IntroChecked As Boolean
    Dim IntroDir As String
    Open Filename For Binary Access Write As #1
    Put #1, , IntroChecked
    Put #1, , IntroDir
    Close #1

    This doesnt seem to work correctly. The string isn't saved correctly for some odd reason but the IntroChecked value is saved correctly.

    I'm thinking that maybe I have to set the IntroDir variable as something other than a string for binary saving. I have no idea.

    Please Help.

    -Simon Bingier

  2. #2
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    what you want to do is use a User defined type

    This is from MSDN


    Opening a File for Binary Access
    To open a file for binary access, use the following syntax for the Open statement:

    Open pathname For Binary As filenumber

    As you can see, Open for binary access differs from Open for random access in that Len = reclength is not specified. If you include a record length in a binary-access Open statement, it is ignored.

    Storing Information in Variable-Length Fields
    To best appreciate binary access, consider a hypothetical Employee Records file. This file uses fixed-length records and fields to store information about employees.

    Type Person
    ID As Integer
    MonthlySalary As Currency
    LastReviewDate As Long
    FirstName As String * 15
    LastName As String * 15
    Title As String * 15
    ReviewComments As String * 150
    End Type

    Regardless of the actual contents of the fields, every record in that file takes 209 bytes.

    You can minimize the use of disk space by using binary access. Because this doesn’t require fixed-length fields, the type declaration can omit the string length parameters.

    Type Person
    ID As Integer
    MonthlySalary As Currency
    LastReviewDate As Long
    FirstName As String
    LastName As String
    Title As String
    ReviewComments As String
    End Type

    Public Empl As Person ' Defines a record.

    Each employee record in the Employee Records file now stores only the exact number of bytes required because the fields are variable-length. The drawback to binary input/output with variable-length fields is that you can’t access records randomly — you must access records sequentially to learn the length of each record. You can seek directly to a specified byte position in a file, but there is no direct way to know which record is at which byte position if the records are of variable length.

    For More Information For additional information on binary file access, see "Open Statement."

    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>



    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$

    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    There's nothing wrong with the string actually, there's just no descriptor written for it so you have to make your own, declare a byte or integer variable (or long) depending on the max size of that string, and put the length of the string len(variablename) into the descriptor, and the put the descriptor before putting the string itself, then when you read it with get:
    Code:
     Dim length as integer
     Get #1,,length
     yourstring=space(length)
     Get #1,,yourstring
    you use for instance space to predefine the length of the string, if you pass a nullstring it will return with the rest of the file.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

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



Click Here to Expand Forum to Full Width