Solved

using put and get.......

Posted on 1999-01-11
12
134 Views
Last Modified: 2010-05-03
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
0
Comment
Question by:xersoft
12 Comments
 
LVL 3

Expert Comment

by:idt
Comment Utility
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
 
LVL 4

Expert Comment

by:chabaud
Comment Utility
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
 
LVL 3

Expert Comment

by:HATCHET
Comment Utility
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
 
LVL 1

Expert Comment

by:yfang
Comment Utility
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
 
LVL 5

Author Comment

by:xersoft
Comment Utility

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
 
LVL 5

Author Comment

by:xersoft
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Author Comment

by:xersoft
Comment Utility
I added 50 points to the question because of the huge change.
0
 
LVL 4

Accepted Solution

by:
chabaud earned 150 total points
Comment Utility
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
 
LVL 5

Author Comment

by:xersoft
Comment Utility
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
 
LVL 4

Expert Comment

by:chabaud
Comment Utility
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
 
LVL 4

Expert Comment

by:chabaud
Comment Utility
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
 
LVL 5

Author Comment

by:xersoft
Comment Utility
Thanks a lot!!!!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now