Reading/Writing Arrays from/to Files
Reading/Writing Arrays from/to Files
Reading/Writing Arrays from/to Files
Reading/Writing Arrays from/to Files
Reading/Writing Arrays from/to Files
Reading/Writing Arrays from/to Files Reading/Writing Arrays from/to Files Reading/Writing Arrays from/to Files Reading/Writing Arrays from/to Files Reading/Writing Arrays from/to Files Reading/Writing Arrays from/to Files Reading/Writing Arrays from/to Files Reading/Writing Arrays from/to Files
Reading/Writing Arrays from/to Files Reading/Writing Arrays from/to Files
Reading/Writing Arrays from/to Files
Go Back  Xtreme Visual Basic Talk > > > Reading/Writing Arrays from/to Files


Reply
 
Thread Tools Display Modes
  #1  
Old 10-14-2013, 06:00 PM
fdates fdates is offline
Newcomer
 
Join Date: Jan 2005
Posts: 18
Default Reading/Writing Arrays from/to Files


I'm trying to find the best/correct way to save array data to a file so that it can be loaded back into the array during a different session.

The array is a fixed dimension of 9 elements (1 to 9), but it contains within each element an array of 6 elements (1 to 6).

Code:
Private Type MA
    
    sValue As String
    bExp As Boolean
    bActive As Boolean

End Type

Private Type MAP

    arrMA(1 To 6) As MA
    
End Type

Public arrMA_Preset(1 To 9) As MAP

Will I have to run each a series of complex loops or is there a technique to handle reads/writes of this type?

Thanks.
Reply With Quote
  #2  
Old 10-14-2013, 11:19 PM
passel's Avatar
passelReading/Writing Arrays from/to Files passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

You won't necessarily have to write loops.
If your arrays were going to be dynamic in size, you would want to write dimensional information to the file first so you could resize the arrays before doing the reads.
But if you have fixed size arrays, like you've shown, and the array is the same fixed size when you read, VB should be able to serialize the arrays to a binary stream of bytes, and then read that same stream of bytes back into the arrays.
For instance, putting your types in a Module
Code:
'Module1
Option Explicit

Private Type MA
    sValue As String
    bExp As Boolean
    bActive As Boolean
End Type

Private Type MAP
    arrMA(1 To 6) As MA
End Type

Public arrMA_Preset(1 To 9) As MAP
Public readBack(1 To 9) As MAP      'added so we can read back from the file and verify it works without having to exit the program and restart
'Or you code put the fill with test data and write under one button, and the read under another and exit the program after the file written,
'Start the program back up, and then read the data back into the same array using the other button.
Code:
'Form1
Option Explicit

Private Sub Form_Load()
  Dim i As Integer
  Dim j As Integer
  
 'Fill the array of structures with some test data
  For i = 1 To 9
    For j = 1 To 6
      With arrMA_Preset(i).arrMA(j)
        .sValue = Str$(i) & "," & Str$(j)
        .bExp = (i = j)
        .bActive = (i <> j)
      End With
    Next
  Next
  
  'Write the array to a file opened in Binary Mode
  Open "tstIt.dat" For Binary As #1
  Put #1, , arrMA_Preset
  Close #1
End Sub

Private Sub Command1_Click()
'Read the data back from the file
  Open "tstIt.dat" For Binary As #1
  Get #1, , readBack
  Close #1

'Test some elements of the array to see if they contain what was in the original array
  Dim i As Integer, j As Integer
  For i = 3 To 6
    For j = 3 To 6
      With readBack(i).arrMA(j)
        Debug.Print .sValue, .bExp, .bActive
      End With
    Next
  Next

End Sub
Code:
'Result in Immediate window
 3, 3         True          False
 3, 4         False         True
 3, 5         False         True
 3, 6         False         True
 4, 3         False         True
 4, 4         True          False
 4, 5         False         True
 4, 6         False         True
 5, 3         False         True
 5, 4         False         True
 5, 5         True          False
 5, 6         False         True
 6, 3         False         True
 6, 4         False         True
 6, 5         False         True
 6, 6         True          False
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 10-14-2013 at 11:25 PM.
Reply With Quote
  #3  
Old 10-23-2013, 05:03 PM
fdates fdates is offline
Newcomer
 
Join Date: Jan 2005
Posts: 18
Default

Ah. Save and read to a binary file. Now that's something I've never done before. This should be interesting.

Since access to the file is only done at startup (to initialize the arrays), I probably would not need the readback array and can use the original arrMA_Preset array.

Thank you passel!
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

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
Reading/Writing Arrays from/to Files
Reading/Writing Arrays from/to Files
Reading/Writing Arrays from/to Files Reading/Writing Arrays from/to Files
Reading/Writing Arrays from/to Files
Reading/Writing Arrays from/to Files
Reading/Writing Arrays from/to Files Reading/Writing Arrays from/to Files Reading/Writing Arrays from/to Files Reading/Writing Arrays from/to Files Reading/Writing Arrays from/to Files Reading/Writing Arrays from/to Files Reading/Writing Arrays from/to Files
Reading/Writing Arrays from/to Files
Reading/Writing Arrays from/to Files
 
Reading/Writing Arrays from/to Files
Reading/Writing Arrays from/to Files
 
-->