Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > File I/O and Registry > This But Not That


Reply
 
Thread Tools Display Modes
  #1  
Old 11-18-2004, 08:05 PM
woody__119 woody__119 is offline
Centurion
 
Join Date: Jun 2004
Posts: 106
Default This But Not That


Hi everyone.
Just wanted to know why

This Works

Open "e:\test.ini" For Input As #1
Input #1, OUTPUTPRINT
Text3.SetFocus
Text3.Text = OUTPUTPRINT
Close #1


But Not This

Open "e:\test.ini" For Binary Access Read Write As #1
Get #1, , OUTPUTPRINT
Text3.SetFocus
Text3.Text = OUTPUTPRINT
Close #1

As I need to write to the file and read as I go I have to use the second opt.
Thanks
Reply With Quote
  #2  
Old 11-20-2004, 07:20 PM
RoofRabbit's Avatar
RoofRabbit RoofRabbit is offline
Contributor
 
Join Date: Sep 2003
Location: Lenoir, NC - USA
Posts: 731
Default

Try changing your second option to this, it will give you read/write access:


Open "e:\test.ini" For Binary As #1
Get #1, , OUTPUTPRINT
Text3.SetFocus
Text3.Text = OUTPUTPRINT
Close #1
Reply With Quote
  #3  
Old 11-20-2004, 07:28 PM
MikeJ's Avatar
MikeJ MikeJ is offline
Retread

Retired Moderator
* Expert *
 
Join Date: Sep 2002
Location: Austin, Texas
Posts: 6,742
Default

__________________
{ Lex Fori } { Locus Classicus } { Rutilus Scrinium }
Osculare pultem meam!
Reply With Quote
  #4  
Old 11-20-2004, 08:45 PM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
Default

The main problem with this

Get #1, , OUTPUTPRINT

is that OUTPUTPRINT has to have a size.
The "Get" will only read as much data from a binary file as it takes to fill the destination
variable.

So if OUTPUTPRINT was a string but was not initialized, then nothing would be read
from the file since the string had no size. If you preset the string to some length (say
set it to Space$(5)), then it would read five characters from the file.

If the data is going to vary in length then you have a problem.

This is a case where the overhead of using a Variant may have some justification.
The reason that variants are normally avoided is because they take up more memory
since they are essentially a record that hold a value of some type (integer, single, string, etc)
plus the additional information necessary to identify the type, and if it is
a string, or array, then the sizes of the dimensions and/or size of the string(s).

In addition to the extra memory overhead, they are slower to access than the native
type because of the need to decode the type the variant contains.

But, if you need to read and write widely variable data to a file and don't want to take
care of creating your own method of tagging the information in a way that you can
read and write mixed data to the file, then using variants is a simple solution. If you
have a variant variable, and you assign a string to it, and then "Put" that variable to a
binary file, the information needed to read that variant variable back in is recorded
as well, as part of the variant.

So the data written will be read back exactly as written with a minimum of fuss when
"Put"ing out a variant variable, and then "Get"ing in a variant variable.
The only "trick" is to use a variant when putting and getting. If you write an integer,
string, or any other non-variant variable (with the exception of User Defined Types),
don't expect to be able to read back into a variant since you would be missing the
variant header information.

If your data was more structured, or relatively consistent, using a User Defined Type (UDT)
could be more efficient. It really depends on the type of things you will be writing.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #5  
Old 11-25-2004, 08:45 PM
woody__119 woody__119 is offline
Centurion
 
Join Date: Jun 2004
Posts: 106
Default Thanks

Thanks for your replie.
I have been trying to setup it using a variant but i cant get it to work.
I understand what you said about it all. But can you please show me an example to do this process of reading using a variant.
Thanks
Reply With Quote
  #6  
Old 11-25-2004, 09:42 PM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
Default

Well I'm not sure how you want to write and read from the file as you go.
By that, I don't know if you want to write from one program and read from the other.
Whether you want to handshake the read and write so only one item is written and
has to be read by the other process before the next item is written. Whether you
want the items to accumulate in the file, etc..

Here's a simple example.
Add two command buttons to a form and paste this code into the declarations area.
Clicking on command1 will add one of three random type variables to the file (an Integer,
a Double, or a String).
Clicking on command2 will read value from the same file.
What is written and what is read is printed in the Debug (Immediate) window.
Don't forget to read the File I/O Tutorial.
Code:
Private Sub Command1_Click() Dim v As Variant Select Case Int(Rnd * 3) ' Write one of three types Case 0: v = 5 Case 1: v = "Test" Case 2: v = 123.45 End Select Put #1, , v Debug.Print "Wrote: "; v End Sub Private Sub Command2_Click() Dim v As Variant If Seek(2) < LOF(2) Then 'if not at the end of the file Get #2, , v ' read another value Debug.Print "Read: "; v; " "; TypeName(v) End If End Sub Private Sub Form_Load() If Len(Dir$("c:\tstr.bin")) > 0 Then Kill "c:\tstr.bin" 'if the file exist, delete it Open "c:\tstr.bin" For Binary Shared As #1 'Use file handle 1 to write to the file Open "c:\tstr.bin" For Binary Shared As #2 'Use file handle 2 to read from the same file End Sub Private Sub Form_Unload(Cancel As Integer) Close End Sub
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->