Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Saving/Loading Arrays

Posted on 2003-03-03
7
Medium Priority
?
193 Views
Last Modified: 2010-04-07
I'd like to Save/Load Arrays in/out of files...
Does anyone know how to do this... maybe something like the old BASIC BLoad/BSave? I'd like to save the contents of a datum? example...

Data(i).Name
Data(i).Value
Data(i).etc ...

Is their an easy way to do this, im sure there is ;)
0
Comment
Question by:tampabay420
7 Comments
 
LVL 1

Author Comment

by:tampabay420
ID: 8057215
This is currently how i save my data (text file), although i'd like to see a more intellegent way of doing this...

[code]
Open oFile For Input As #1
    Do While Not EOF(1)
        Input #1, nline
        X = Split(nline, "|")
    For k = 0 To Fields
        If X(0) = crfdata(k).name Then crfdata(k).value = X(1)
    Next k
    Loop
Close #1

'################################################################

Dim SaveName As String
main
Sheet1.loadfieldnames
SaveName = "c:\faorientation\autorecover.crf"
Open SaveName For Output As #1
    For i = 0 To Fields
        If crfdata(i).value <> "" And crfdata(i).name <> "" Then Print #1, Trim(crfdata(i).name & "|" & Replace(crfdata(i).value, Chr$(13), ""))
    Next i
Close #1
[/code]
0
 
LVL 5

Expert Comment

by:Cimperiali
ID: 8057922
You can also deal with random files (using data types) or even with binary files;
A trick sometimes I used when I was in a hurry, was the following:
'***************************************************
Warning: the following is *NOT* really professional!
'***************************************************
Option Explicit

Private Sub Command1_Click()
Dim myArray(10) As String
Dim thePrintable As Variant
Dim intCounter As Integer
For intCounter = 0 To 10
   myArray(intCounter) = "Item " & intCounter + 1
Next
'thePrintable = myArray
Open "c:\myTest.txt" For Binary As #1
   Put #1, 1, myArray
Close #1
End Sub
Private Sub Command2_Click()
Dim myArray As String
Dim theResult() As String
myArray = Space(FileLen("c:\myTest.txt"))
Open "c:\myTest.txt" For Binary As #1
   Get #1, 1, myArray
Close #1
myArray = Right(myArray, Len(myArray) - 2)
myArray = Replace(myArray, Chr(0), "")
myArray = Replace(myArray, Chr(6), vbCrLf)
myArray = Replace(myArray, Chr(7), vbCrLf)
theResult = Split(myArray, vbCrLf)

End Sub
 
0
 
LVL 4

Expert Comment

by:TigerZhao
ID: 8061521
if define UDT in public object module(public class in dll, etc), use PropertyBag is easy

Public Type UDT
    Name As String
    Value As Variant
    '...
End Type

Sub SaveArray(ByVal FileName As String, Data() As UDT)
    Dim pb As PropertyBag
    Dim ba() As Byte 'Byte Array
    Dim nFN As Integer 'FileNumber
   
    'Data to ByteArray
    Set pb = New PropertyBag
    pb.WriteProperty "Data()", Data
    ba = pb.Contents
    Set pb = Nothing
    'Save to file
    nFN = FreeFile()
    Open FileName For Binary Access Write Lock Write As #nFN
    Put nFN, 1, ba
    Close nFN
End Sub

Function LoadArray(ByVal FileName As String) As UDT()
    Dim pb As PropertyBag
    Dim ba() As Byte 'Byte Array
    Dim nFN As Integer 'FileNumber
   
    'load from file
    nFN = FreeFile()
    Open FileName For Binary Access Read Lock Write As #nFN
    Get nFN, 1, ba
    Close nFN
    'ByteArray to Data
    Set pb = New PropertyBag
    pb.Contents = ba
    LoadArray = pb.ReadProperty("Data()")
End Function

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:R2DTOEA
ID: 8105560
This is similiar to the above, though different enough.
I get the entire file all at once like:

    intHandle = FreeFile
    Open strFileName$ For Binary As #intHandle
    strFileData = Space$(LOF(intHandle))
    Get #intHandle, , strFileData
    Close #intHandle

It is a lot quicker than looping if the file is big.
Then to put it into an array:

'This will split the file into an array based upon a pipe delimiter.
aryItems = split(strFileData, "|")

To get it back to a file:
strFileData = Join(aryItems, "|")
intHandle = FreeFile
open strFileName$ for Output as #intHandle
print #intHandle, strFileData
close #intHandle
0
 

Expert Comment

by:CleanupPing
ID: 8900856
tampabay420:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 9012145
tampabay420, an EE Moderator will handle this for you.
Moderator, my recommended disposition is:

    Save as PAQ -- No Refund.

DanRollins -- EE database cleanup volunteer
0
 

Accepted Solution

by:
PashaMod earned 0 total points
ID: 9146297
Per recommendation,

PashaMod
CS Moderator
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question