473,287 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

REQ: Declaring variables and file for random access

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
10 6295
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

"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
Steve,

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

Stefanie
Jul 17 '05 #4
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

"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
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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John F Dutcher | last post by:
Can anyone comment on why the code shown in the Python error is in some way incorrect...or is there a problem with Python on my hoster's site ?? The highlites don't seem to show here...but line...
2
by: Oliver Corona | last post by:
I am wondering if anyone has any insights on the performance benefit (or detriment) of declaring local variables instead of referencing members. Is allocating memory for a new variable more...
1
by: ColinWard | last post by:
Hi guys. I have a question about declaring variables. I do a lot of re-querying of controls in my database and I use the Set statement with a variable set to the name of the control to tell the...
8
by: Radu Colceriu | last post by:
HI, I've an asp.net app like this: login.aspx (no frame) :- save in session the user and pass -> framedoc.html :- frameset 2 content 1. menu.aspx...
27
by: Madhav | last post by:
Hi all, I did not understand why do the global vars are initialized to NULL where as the block level variables have random values? I know that the C standard requires this as was mentioned in a...
3
by: Bruce | last post by:
I am building a WinForms app that uses Web Services access to a server for most of its data input/output, but I also need to persist some of its data to the local disk (basically as a cache of some...
16
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
16
by: Steven D'Aprano | last post by:
On Tue, 09 Sep 2008 14:59:19 -0700, castironpi wrote: You've created a solution to a problem which (probably) only affects a very small number of people, at least judging by your use-cases....
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.