using put and get.......

I declare a type like as follows
type testing
test as byte
test2 as string
test3 as integer
test4 as byte
end type

Then I dim a var.
global info as testing

later after setting valus in all the fields I try to put the var to a file like this

Open "c:\test" For Binary As #1 Len = 30000
Put #1, 1, info()
Close #1

When I get the info back into memory the values are all messed up. I am sure this is because of the strings. When I take them out the values are fine. But I want to put information with strings into these binary files. If I need to be clearer please say...
Thanks
LVL 5
xersoftAsked:
Who is Participating?
 
chabaudConnect With a Mentor Commented:
The second parameter of put is the byte position in the binary file, In you code, the second put overwrite the first one.

Put #1, 1, sprites()
Put #1, 1000& * LenB(sprites(0)), custombutton()

and the same for get:

Get #1, 1, sprites()
Get #1, 1000& * LenB(sprites(0)), custombutton()

0
 
idtCommented:
well..
The Len clause is ignored if mode is Binary.

Put writes elements of user-defined types as if each were written individually, except there is no padding between elements.

Yes the string is giving you grief, change the string var to a fixed len string.

You didn't incluse the get statement you were reading with
0
 
chabaudCommented:
as idt says, use fixed string as follow, and you can open your file for random specifing your record size.

type testing
  test as byte
  test2 as string * 64
  test3 as integer
  test4 as byte
end type

global info as testing
dim recnumber as integer

recnumber = 1

Open "c:\test" For Random As #1 Len = lenB(info)
Put #1, recnumber, info
Close #1


0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
HATCHETCommented:
xersoft,

I'm sitting here trying to figure out what you're trying to write to disk and what is wrong with your code to give a suggestion on fixing your problem... but I HAVE NO CLUE what you're trying to write to disk, and looking at your code doesn't help.  If you told me what it is you're trying to write to disk I could give you an example of how to correctly write it and read it.

HATCHET
0
 
yfangCommented:
I guess you have to explicitly declare how many characters you string would contain, as Chabaud has suggested.  The reason is that a string can contain variable of bytes.  If you string has variable length, use a function to make all the string elements the same length.  I am also confused by your use of Byte varialbe.  Normally, this should be a varialbe used to READ information from a binary file.  If you want to write a one digit number to your file, why not use integer?
0
 
xersoftAuthor Commented:

yfang:
I use the byte data type because I figured it would take up less memory then the integer type. I only needed say 10 or 20 numbers and a byte can store up to 255


chabaud:
I don't understand the part   Len = lenB(info)     in your open statment. I think that lenB(info) can just be subtuted with 66 since the string can not be more then 64. But I don't know for sure. I'll give it a try and see what happens.
0
 
xersoftAuthor Commented:
Ok for get all the string stuff. I have decided to go another way. I don't really want to work with strings for they are to uncerten. I took care of them nother way. But... I still seem to be haveing problems. Please take a look at this code and tell me what you think is wrong or how it can be impoved.

**********************************************************************************************

I have two varables declared with the following var types

sprites(1000) as sprite
custombutton(25) as custom_button



Type sprite
    directions1(16) As Integer
    ai As Byte
    text As Integer
    frames As Byte
    speeds As Byte
End Type

Type custom_button
    left As Integer
    top As Integer
    width1 As Integer
    height1 As Integer
    tag_true As Integer
    tag_false As Integer
    visible As Integer
    Border1 As Integer
    alin As Integer
    texttop As Integer
    textleft As Integer
End Type

I try to save then like this....
open "c:\test.tst" for binary as #1
put #1,1,sprites()
put #1,2,custombutton()
close #1

Then I try to open them like this...
open "c:\test.tst" for binary as #1
get #1,1,sprites()
get #1,2,custombutton()
close #1

0
 
xersoftAuthor Commented:
I added 50 points to the question because of the huge change.
0
 
xersoftAuthor Commented:
chabaud: You have the points! I'll add 50 more if you can show me how to add more varables onto this I think it would be done like this but I'm not sure:

Put #1, 1, sprites()
Put #1, 1000& * LenB(sprites(0)), custombutton()
Put #1, 1000&*(LenB(sprites(0)) + LenB(custombutton(0))),nextvarable()

If this is wrong then please say. If you wish not to answer any more of this question just say and I will give you your 150 you already deserve. Thanks a lot for all the help!
0
 
chabaudCommented:
The correct lines are:

Put #1, 1, sprites()
Put #1, 1000& * LenB(sprites(0)), custombutton()
Put #1, 1000&*(LenB(sprites(0))) + 25& * LenB(custombutton(0)),nextvarable()

Here is a schema of your file:

+-----------------+ <-- bytenumber=1
| sprites(0)      |
|         .       |
|         .       |
|         .       | block size= 1000& * LenB(sprites(0))
|         .       |
|         .       |
| sprites(999)    |
+-----------------+ <-- bytenumber= 1000& * LenB(sprites(0))
| custombutton(0) |
|              .  |
|              .  | block size= 25& * LenB(custombutton(0))
|              .  |
| custombutton(25)|
+-----------------+ <-- bytenumber= 1000& * LenB(sprites(0)) + 25& * LenB(custombutton(0))

| nextvarable(0)  |
|             .   |
|             .   |
|             .   |
+-----------------+
|                 |



0
 
chabaudCommented:
For easy coding use a bytepos variable and the generic ubound and lbound functions:

Dim bytepos as long

bytepos = 1

Put #1, bytepos, sprites()
bytepos = bytepos + (ubound(sprites) - lbound(sprites) +1) * LenB(sprites(0))

Put #1, bytepos, custombutton()
bytepos = bytepos + (ubound(custombutton) - lbound(custombutton) +1) * LenB(custombutton(0))

Put #1, bytepos ,nextvarable()
bytepos = bytepos + (ubound(nextvarable) - lbound(nextvarable) +1) * LenB(custombutton(0))

0
 
xersoftAuthor Commented:
Thanks a lot!!!!!
0
All Courses

From novice to tech pro — start learning today.