473,545 Members | 937 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6312
In message <15************ **************@ news1.zonnet.nl >, Stefanie
<St******@zsmai l.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(inputstrin g))

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******@zsmai l.net> wrote in message
news:15******** *************** ***@news1.zonne t.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 = FixedLengthNeed ed
j = Len(StringToWri te)

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$(StringToW rite, 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******@zsmai l.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******@zsmai l.net> wrote in message
news:38******** *************** ****@news1.zonn et.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*****@nowher e.uk> wrote in message
news:41******** *******@news.bt click.com...
: On Mon, 6 Dec 2004 11:49:27 +0100, "Stefanie" <St******@zsmai l.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******@zsmai l.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6075
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 #80 and line # 38 are the first line offenders. --> --> -->
2
2129
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 efficient than repeatedly referencing the member in a loop? Maybe using a string isn't the best example, but hopefully you get the idea! * example...
1
2415
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 program which control to requery. This makes it easy to requery controls on a different form. However, up until today, I was dimming a new local...
8
2281
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 2.docviewver.aspx
27
2779
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 recent thread. I want to know why this descrimination is in place. Can't all the variables be initialised to NULL automatically by the compiler?...
3
3010
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 of the Web Services data) in XML format. Since the size of the XML local store could be rather large, I'd prefer to have a random access...
16
7157
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 unchanged). How can I do that in Python? Claudio Grondi
8
7499
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, like this: for(var i=0; i<list; i++) { var name = list; }
16
1597
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. Who has a 4GB XML file, and how much crack did they smoke? Castironpi, what do *you* use this proof-of-concept module for? Don't
0
7401
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7807
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7756
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5326
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3442
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1879
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.