Solved

using put and get.......

Posted on 1999-01-11
12
148 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 3

Expert Comment

by:idt
ID: 1455560
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
ID: 1455561
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
ID: 1455562
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 1

Expert Comment

by:yfang
ID: 1455563
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
ID: 1455564

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
ID: 1455565
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
 
LVL 5

Author Comment

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

Accepted Solution

by:
chabaud earned 150 total points
ID: 1455567
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
ID: 1455568
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
ID: 1455569
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
ID: 1455570
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
ID: 1455571
Thanks a lot!!!!!
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

615 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