Link to home
Create AccountLog in
Avatar of Shackman
Shackman

asked on

Get and Put, Again

Ok, i have some code and I have no idea whats wrong with it.

Private Sub Command1_Click()
Open "RSR" For Binary As #1
file4 = "Ding"
Put #1, , file4
Get #1, , file3
Close #1
RichTextBox2.Text = file3
End Sub

I checked and the put statement works, but the get statements reads the file and comes out with a null string.  When you look at the file in notepad it does say "Ding".  By the way, all the file3 and file4 are declared as strings.  Thanks for the help.
Avatar of Vbmaster
Vbmaster

To read a string variable using Get you will need to dimension the string first, this can be done using Space$(length_of_string). The Get statement will then read length_of_string characters and put it into the string.
To show a example...

   Private Sub Command1_Click()

      Open "RSR" For Binary As #1
      file4 = "Ding"
      Put #1, , file4
      Seek #1, 1
      file3 = Space$(4)
      Get #1, , file3
      Close #1
      RichTextBox2.Text = file3

   End Sub

This code will load the "Ding" string into file3, probably what you tried to do.
When you first open the RSR file, it is blank.  After you Put the file4 in, the RSR will contain "Ding".  Because you did not close or reset the cursor back to the beginning, the cursor will be point after the char "g".  If you try to read in file3, you will get a null string because there is no other string after "Ding".  To retrieve the string "Ding", first close the file, then open again and read.

The "Seek" command in VBmaster could work as it looks like will (I never try that before though) reset the cursor to the beginning .

Hope this will give you a general idea what happened.

cdloves
ASKER CERTIFIED SOLUTION
Avatar of GivenRandy
GivenRandy

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer