473,387 Members | 1,757 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,387 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 6299
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....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.