By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

REQ: Declaring variables and file for random access

P: n/a
Hi,

I have a question about declaring variables for writing data to a file for
random access.
I have two string variables declared in a module (user-defined types).
The point is that for one of them i can't define (hard coded) a fixed
length because the application is reading data
from another file (In that file they have the same lenght).
Is it possible to use the lenght i obtain from reading that other file to
declare my variable or are there
other possibilities.

Thanks in advance,

Stefanie
Jul 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
In message <15**************************@news1.zonnet.nl>, Stefanie
<St******@zsmail.net> writes
file they have the same lenght). Is it possible to use the lenght i obtain from
reading that other file to
declare my variable or are there
other possibilities.


Off the top of my head. You can define a dynamic array within a user
defined type:-

Type mytype
....
mystring()
...
End Type

You can then set the size of mytype.mystring using:-

Redim mytype.mystring (Len(inputstring))

However, you may be storing up trouble if you want to use random access
on the resulting file. Where do you seek to for a new record?
HTH.

Regards

--
Martin Trump
Jul 17 '05 #2

P: n/a

"Stefanie" <St******@zsmail.net> wrote in message
news:15**************************@news1.zonnet.nl. ..
| Hi,
|
| I have a question about declaring variables for writing data to a file
for
| random access.
| I have two string variables declared in a module (user-defined types).
| The point is that for one of them i can't define (hard coded) a fixed
| length because the application is reading data
| from another file (In that file they have the same lenght).
| Is it possible to use the lenght i obtain from reading that other file
to
| declare my variable or are there
| other possibilities.
|
| Thanks in advance,
|
| Stefanie
|

I think what you are after is how to write a string to an output file
with a specified field width, without using a fixed length string, since
you don't know the fixed length needed until run time.

If so, then you should be able to do something like this:
n = FixedLengthNeeded
j = Len(StringToWrite)

k = n - j ' spaces needed

If k > 0 Then
' print string and pad with spaces
Print #FileNum, StringToWrite, Space$(k)
Else
' clip string to maximum length
Print #FileNum, Left$(StringToWrite, n)
End If
Jul 17 '05 #3

P: n/a
Steve,

Thats the same as writing data to the file using Put, right ?

Stefanie
Jul 17 '05 #4

P: n/a
On Mon, 6 Dec 2004 11:49:27 +0100, "Stefanie" <St******@zsmail.net>
wrote:
Steve,

Thats the same as writing data to the file using Put, right ?


I could not see your first post

Below is a demo of Random Access using UDTs containing variable length
strings - the Record Length that it is opened with has to be large
enough.
- I would not do it like this personally

Option Explicit

Private Type TRec
L As Long
S As String
I As Integer
End Type

Private Sub Command1_Click()
Dim Rec As TRec, Channel%

Rec.L = -1
Rec.S = "Test String"
Rec.I = -1

Channel = FreeFile
Open App.Path + "\test.dat" For Random As Channel Len = 200
Put #Channel, 1, Rec
Close #Channel

End Sub

Private Sub Command2_Click()
Dim Rec As TRec, Channel%

Channel = FreeFile
Open App.Path + "\test.dat" For Random As Channel Len = 200
Get #Channel, 1, Rec
Close #Channel
Me.Print Rec.L, Rec.S, Rec.I

End Sub

Jul 17 '05 #5

P: n/a

"Stefanie" <St******@zsmail.net> wrote in message
news:38***************************@news1.zonnet.nl ...
| Steve,
|
| Thats the same as writing data to the file using Put, right ?
|
| Stefanie
|

I'm hoping you have access to help files and can look up Put. Its
behavior with variable length strings varies according to whether they
are in a UDT, and whether the file is opened Random or Binary.

As far as I can tell, Put will write a 2 byte length before writing a
variable length string to a file opened Random (UDT or not), which
allows Get to retrieve it correctly. The total record length would have
to be long enough for that. I don't believe you actually have to pad out
the string length unless you want to.

Jul 17 '05 #6

P: n/a
Interesting! I tried something similar yesterday, and got a 'bad record
length error when trying to do the Put.

--
Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/
"J French" <er*****@nowhere.uk> wrote in message
news:41***************@news.btclick.com...
: On Mon, 6 Dec 2004 11:49:27 +0100, "Stefanie" <St******@zsmail.net>
: wrote:
:
: >Steve,
: >
: >Thats the same as writing data to the file using Put, right ?
: >
:
: I could not see your first post
:
: Below is a demo of Random Access using UDTs containing variable length
: strings - the Record Length that it is opened with has to be large
: enough.
: - I would not do it like this personally
:
: Option Explicit
:
: Private Type TRec
: L As Long
: S As String
: I As Integer
: End Type
:
: Private Sub Command1_Click()
: Dim Rec As TRec, Channel%
:
: Rec.L = -1
: Rec.S = "Test String"
: Rec.I = -1
:
: Channel = FreeFile
: Open App.Path + "\test.dat" For Random As Channel Len = 200
: Put #Channel, 1, Rec
: Close #Channel
:
: End Sub
:
: Private Sub Command2_Click()
: Dim Rec As TRec, Channel%
:
: Channel = FreeFile
: Open App.Path + "\test.dat" For Random As Channel Len = 200
: Get #Channel, 1, Rec
: Close #Channel
: Me.Print Rec.L, Rec.S, Rec.I
:
: End Sub
:

Jul 17 '05 #7

P: n/a
On Mon, 6 Dec 2004 22:36:52 -0500, "Randy Birch"
<rg************@mvps.org> wrote:
Interesting! I tried something similar yesterday, and got a 'bad record
length error when trying to do the Put.


Yes, it will fail if the data is wider than the 'record length'
- something that needs to be manually checked

Personally I would be /very/ wary of using it

IMO data written to disk should be 'language independent'
Jul 17 '05 #8

P: n/a
Steve,
As far as I can tell, Put will write a 2 byte length before writing a
variable length string to a file opened Random (UDT or not), which
allows Get to retrieve it correctly. The total record length would have
to be long enough for that. I don't believe you actually have to pad out
the string length unless you want to.


Yes i read that too, but i don't understand how to do this:

This are the variables declared in my module.

Type Record
vDateTime As String * 19
vFileName As String
End Type

Public RecVar As Record

Using vFileName As String * 255 gives my enough for een full path, but most
of the time it's wasting space.

I use this for opening my file:

Open strFileName For Random As #Dnr Len = Len(RecVar)

But it's gives an error. "Bad record lenght"

Maybe somebody can offer me an example how to use the aforesaid.

Stefanie
Jul 17 '05 #9

P: n/a
On Thu, 9 Dec 2004 12:38:00 +0100, "Stefanie" <St******@zsmail.net>
wrote:
Steve,
As far as I can tell, Put will write a 2 byte length before writing a
variable length string to a file opened Random (UDT or not), which
allows Get to retrieve it correctly. The total record length would have
to be long enough for that. I don't believe you actually have to pad out
the string length unless you want to.


Yes i read that too, but i don't understand how to do this:

This are the variables declared in my module.

Type Record
vDateTime As String * 19
vFileName As String
End Type

Public RecVar As Record

Using vFileName As String * 255 gives my enough for een full path, but most
of the time it's wasting space.

I use this for opening my file:

Open strFileName For Random As #Dnr Len = Len(RecVar)

But it's gives an error. "Bad record lenght"

Maybe somebody can offer me an example how to use the aforesaid.


Look at the example I posted earlier in this thread

If you are interested in not wasting space then look carefully at
opening the file in Binary mode rather than Random mode

Have a careful look at the Seek Statement, and more importantly the
Seek Function.
Jul 17 '05 #10

P: n/a

"Stefanie" <St******@zsmail.net> wrote in message
news:37***************************@news1.zonnet.nl ...
| Steve,
|
| > As far as I can tell, Put will write a 2 byte length before writing
a
| > variable length string to a file opened Random (UDT or not), which
| > allows Get to retrieve it correctly. The total record length would
have
| > to be long enough for that. I don't believe you actually have to pad
out
| > the string length unless you want to.
|
| Yes i read that too, but i don't understand how to do this:
|
| This are the variables declared in my module.
|
| Type Record
| vDateTime As String * 19
| vFileName As String
| End Type
|
| Public RecVar As Record
|
| Using vFileName As String * 255 gives my enough for een full path, but
most
| of the time it's wasting space.
|
| I use this for opening my file:
|
| Open strFileName For Random As #Dnr Len = Len(RecVar)
|
| But it's gives an error. "Bad record lenght"
|
| Maybe somebody can offer me an example how to use the aforesaid.
|
| Stefanie
|

Len(RecVar) will not include any space for the vFileName string if it is
declared as variable length, hence the bad record length error. You
would have to make the Len in the Open statement equal to the longest
length you expect: approx. Len(RecVar)+255.

If you don't want to reserve 255 for every vFileName, you can't really
use a fixed record length, which means working in Binary rather than
Random. It gets a little more complicated, but Jerry French can help you
out if you need it. Me, I would just waste some disk space, it seems
cheap enough, and go with a fixed length record. 512 sounds like a nice
size.

Jul 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.