Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Saving to Binary
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
GlobeTrotter




PostPosted: Fri Mar 11, 2005 12:58 pm   Post subject: Saving to Binary

Rather than saving to a text file, I'd like to save to a binary file. Can anyone tell me how this is done? Please?
Sponsor
Sponsor
Sponsor
sponsor
Brightguy




PostPosted: Fri Mar 11, 2005 8:33 pm   Post subject: Re: Saving to Binary

No problem...
code:
Open "C:\filename.bin" For Binary As #intFileNum
     Put #intFileNum, byteNumber, variable
Close #intFileNum

"byteNumber" is where in the file you want to write the data in "variable".
GlobeTrotter




PostPosted: Sat Mar 12, 2005 1:05 pm   Post subject: (No subject)

Does it have to be a .bin file?
Brightguy




PostPosted: Sat Mar 12, 2005 7:26 pm   Post subject: Re: Saving to Binary

Nope, you can choose any filename that you want. If the file doesn't exist it will be created.
GlobeTrotter




PostPosted: Sun Mar 20, 2005 8:45 pm   Post subject: (No subject)

Another question, similar to this... how would I get a popup thing/file browser so that the user can choose where they want the file saved?
Brightguy




PostPosted: Mon Mar 21, 2005 12:14 am   Post subject: Re: Saving to Binary

You can use the CommonDialog control for that. Press Ctrl+T to bring up the components dialog box, and select "Microsoft Common Dialog Control" to add the control into your project.

To show a "Save As" dialog box, use the ShowSave method. The user's selection is returned in the FileName property.
GlobeTrotter




PostPosted: Tue Mar 22, 2005 7:33 pm   Post subject: (No subject)

Thanks, I kind of got it working with this code:

code:

Private Sub cmdSave_Click()
    Dim i As Integer
    Dim Var As String
    Var = "Testing"
    cdSaveOpen.ShowSave
   
    If Len(cdSaveOpen.FileName) > 0 Then
        Open cdSaveOpen.FileName For Binary As #1
        Put #1, 1, Var
        Close #1
    End If
   
End Sub


But how do I make it so that they can only save it as a certain file type?
Same thing with opening files.

I'd also like to know if this code is right...

code:


Private Sub cmdOpen_Click()
    Dim i As Integer
    Dim Var As String
    cdSaveOpen.ShowOpen
   
    If Len(cdSaveOpen.FileName) > 0 Then
        Open cdSaveOpen.FileName For Binary As #1
        Get #1, 1, Var
        Close #1
    End If
   
End Sub



When I save a file with the first code, and try opening it with the second, Var does not get the value "testing". Is Get the right command?

Thanks for all the help so far, btw
Brightguy




PostPosted: Thu Mar 24, 2005 10:52 pm   Post subject: Re: Saving to Binary

What you have is correct. However, since Var is a variable-length string, using Get will read the number of characters in Var (which in that case was 0). A solution would be to define Var as a fixed-length string:
code:
Dim Var As String * 7

Another solution would be to open the file in Random mode, where all data is stored in numbered "records", and it will automatically store the length of variable-length strings. (When using Get in random access mode, rather than the byte number you use the record number you want to read.)

As for opening/saving a specific file type, set the Filter property, like so:
code:
cdSaveOpen.Filter = "Text File (*.txt)|*.txt"
Sponsor
Sponsor
Sponsor
sponsor
GlobeTrotter




PostPosted: Thu Mar 24, 2005 11:24 pm   Post subject: (No subject)

Thanks.

Say I wanted to save a file that included a bunch of variables, such as Name, Address, etc. The file would only hold one person's stuff, not a bunch. therefore, I guess the records wouldn't be the right use, correct? What should I do?
Brightguy




PostPosted: Sat Mar 26, 2005 12:24 pm   Post subject: Re: Saving to Binary

You could just have each variable stored in a separate record. By default all records are stored with 127 bytes I believe (so you'll get an error if a string goes over that). But you can change it by adding Len = recordlength at the end of your open statement.

Or you could declare your own "Person" type that includes all the variables you want to store, and then store all the variables in a single record.
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: