Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > File I/O and Registry > Write 2D array to a file


Reply
 
Thread Tools Display Modes
  #1  
Old 11-08-2003, 10:32 AM
Dolptrmn Dolptrmn is offline
Regular
 
Join Date: Nov 2003
Location: Serbia and Montenegro
Posts: 64
Arrow Write 2D array to a file


I need a code which will write any 2D array of strings to a file (and code which will read it).
Is there a way to do it at all?
Reply With Quote
  #2  
Old 11-08-2003, 11:01 AM
Iceplug's Avatar
Iceplug Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
Default

Yes. Assuming you already know how to write a file, you are going to need to find the bounds of the two dimensions with the UBound and LBound functions.
UBound(arr, 1) finds the last number in the first dimension.
UBound(arr, 2) finds the last number in the second dimension.
You can then just set up two looping variables in two For loops that will go through this whole array.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #3  
Old 11-08-2003, 12:28 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

For the price of some User Defined Type overhead, using a UDT can give
you some flexibility in reading and writing arrays and dynamic strings to
a file.

The UDT will store the dimensions of arrays, and the lengths of
dynamic strings as part of its structure. It will use that information to
dynamically resize the arrays and strings when that information is read
back from a file.

So you can write the UDT in a single "put", and read back the UDT in a
single "get" from a file opened for binary input/output.

This example requires two command buttons.
The example defines a UDT to hold a dynamic array of dynamic strings.
This example will use one command button to declare a variable of this
type, and create a two dimensioned array of strings, and write the UDT
variable to a file opened for binary input/output.

The second button will then declare another variable of the UDT type.
It will open the file, and read the UDT into this variable.
The array and strings in the UDT will automatically be sized to hold the
information.
It will then loop through the dynamic array and print the strings to the
debug (immediate) window to show that it worked.

Code:
Private Type StringArrayType 'Define a User Defined Type (UDT) str() As String 'to hold a dynamic array of dynamic strings End Type Private Sub Command1_Click() Dim I As Integer, J As Integer Dim StrAry As StringArrayType 'Create a variable of the Dynamic string array type ReDim StrAry.str(1 To 3, 1 To 5) 'Make it a 3 x 5 array For I = 1 To 3 'For i = range of 1st dimension For J = 1 To 5 ' For j = range of 2nd dimension StrAry.str(I, J) = "I=" & str$(I) & ", J=" & str$(J) 'Store a string "I= i, J= j", Next 'where i = value of I and j = value of J Next Open "c:\tst.dat" For Binary As #1 'open a file for binary input/output Put #1, , StrAry ' Write the UDT variable to the file Close #1 'Close the file End Sub Private Sub Command2_Click() Dim I As Integer, J As Integer Dim NewAry As StringArrayType 'Declare a variable of the dynamic string array type Open "c:\tst.dat" For Binary As #1 'Open the file Get #1, , NewAry ' Get the variable Close #1 'Close the file For I = LBound(NewAry.str, 1) To UBound(NewAry.str, 1) For J = LBound(NewAry.str, 2) To UBound(NewAry.str, 2) Debug.Print NewAry.str(I, J) Next Next 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
  #4  
Old 11-08-2003, 12:43 PM
FireXtol FireXtol is offline
Centurion
 
Join Date: Nov 2003
Posts: 149
Default

A note about saving UDT's with passel's methods.

The original array which points to the UDT, must have it's bounds defined. It will not redimension it for you, and it'll error.

You can still use a dynamic array to point to the UDT. But make sure you redim the array to the number of UDT's first.
__________________
FireXtol
VB6, C++, HTML, Iptscrae
Reply With Quote
  #5  
Old 11-08-2003, 07: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

I wasn't proposing having an array of UDT's. I was proposing having a
single UDT which holds the array of strings. That way the array, being
part of the UDT, will be dimensioned automatically, as are the strings in
the array. My example saves and restores a two dimension array of
dynamic strings, contained in a single UDT.

As you noted, if you save an array, or strings outside of a UDT, then
you will have to know the dimensions of the array, or the lengths of the
strings, and pre-define them, in order for the "get" to return the proper
number of bytes to fill the array or strings.

Putting the array, and/or strings inside a UDT, allows Visual Basic to
keep track of the dimension of the arrays and the length of the strings.
Visual Basic will then dimension and fill the arrays and strings (in the
UDT) based on what was written to the file (for that UDT).

For an example of what FireXtol is talking about, I've modified the
example to use an array of UDTs. It's an array of 3 elements. Each
element (UDT) has a different sized 2 dimensional array of dynamic
strings in it (First element is 3x5, second element is 2x4, third element is
4x4). Since we are writing the array of UDTs to the file, we must
dimension the array (Dim NewAry(1 To 3) As StringArrayType) when we
want to read the array back from the file, because that information is
not saved in the file. But all the arrays, and strings within the UDTs are
restored properly, because the array dimensions and string sizes are
stored, in addition to the data, within the UDTs.

Here's the modified code.
Code:
Private Type StringArrayType 'Define a User Defined Type (UDT) str() As String 'to hold a dynamic array of dynamic strings End Type Private Sub Command1_Click() Dim I As Integer, J As Integer Dim StrAry(1 To 3) As StringArrayType 'Create a variable of the Dynamic string array type ReDim StrAry(1).str(1 To 3, 1 To 5) 'This element will contain a 3 x 5 array of strings ReDim StrAry(2).str(1 To 2, 1 To 4) 'This element will contain a 2 x 4 array of strings ReDim StrAry(3).str(1 To 4, 1 To 4) 'This element will contain a 4 x 4 array of strings 'For each element in our array of 3 UDT's ' For whatever bounds our first dimension has ' For whatever bounds our second dimension has ' Set the String in the two dimensional array to a string identifying ' which UDT, 1st index and 2nd index of this string ' Next element in our second dimension ' next element in our first dimension 'next UDT in our array of UDT's For k = 1 To 3 For I = LBound(StrAry(k).str, 1) To UBound(StrAry(k).str, 1) For J = LBound(StrAry(k).str, 2) To UBound(StrAry(k).str, 2) StrAry(k).str(I, J) = "UDT #" & str$(k) & ": I=" & str$(I) & ", J=" & str$(J) Next Next Next Open "c:\tst.dat" For Binary As #1 'open a file for binary input/output Put #1, , StrAry ' Write the array of UDTs to the file Close #1 'Close the file End Sub Private Sub Command2_Click() Dim I As Integer, J As Integer Dim NewAry(1 To 3) As StringArrayType 'Declare an array of UDT's the same dimensions as was stored in the file Open "c:\tst.dat" For Binary As #1 'Open the file Get #1, , NewAry ' Get the array Close #1 'Close the file 'For each element in our array of 3 UDT's ' For whatever bounds our first dimension has ' For whatever bounds our second dimension has ' Print the string in the UDT, defined by the two dimensional indexes ' Next element in our second dimension ' next element in our first dimension 'next UDT in our array of UDT's For k = 1 To 3 For I = LBound(NewAry(k).str, 1) To UBound(NewAry(k).str, 1) For J = LBound(NewAry(k).str, 2) To UBound(NewAry(k).str, 2) Debug.Print NewAry(k).str(I, J) Next Next Next End Sub

Now this is a more complex situation than I usally deal with.
My original point was to use a single UDT to hold the two dimensioned
array of strings and do a single get or put to save and restore the
data.
But, as you can see here, even with this complexity (an array of UDTs,
with differing dimensioned arrays of strings), it still comes down to a
single Get or Put, to save or restore the data, you just need to keep
track of the size of the array yourself (you could put that information
into the file yourself, rather than hardcode it, but I'm not going to go
into how to do that. You would come up with a scheme to write and
read the additional information you need to the file).
__________________
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Standards and Practices loquin Tutors' Corner 10 07-28-2006 12:16 PM
Installation Problem - PLs help urgenlty dpdsouza Installation / Documentation 4 12-02-2004 07:09 PM
Array and String Functions rhawke General 5 07-10-2003 02:33 AM

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