Saving/Loading Arrays

Posted on 2003-03-03
Medium Priority
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).etc ...

Is their an easy way to do this, im sure there is ;)
Question by:tampabay420
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Author Comment

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

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
Close #1


Dim SaveName As String
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

Expert Comment

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

Expert Comment

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

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

Expert Comment

ID: 8900856
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?
LVL 49

Expert Comment

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

Accepted Solution

PashaMod earned 0 total points
ID: 9146297
Per recommendation,

CS Moderator

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

765 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