• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

Input # write to array problem

I am writing several arrays to a comma-delimited text file, and that is working fine.  However, I realize one cannot use Input # to read that info back into those arrays.  Does anybody have any nifty tricks to read such a file so that I can re-populate the same arrays the file was written from?  Thanks.
--Vingamel

Open "JARNATH_SAVE" For Output As #1   ' Open file for output.
       
        NumberofPassengerTypes,
        For iY = 0 To 3599
            Write #1, MapTiles(3, iY),
        Next iY
0
Vingamel
Asked:
Vingamel
1 Solution
 
Erick37Commented:
Try this:

Dim vA
Dim temp
Dim i As Integer
vA = Array("one", "two", "three", "four", "five")
Dim ff As Integer

ff = FreeFile
Open "c:\test.dat" For Output As #ff
For i = 0 To UBound(vA)
    Write #ff, vA(i);
Next
Close #ff

ff = FreeFile
Open "c:\test.dat" For Input As #ff
Do While Not EOF(ff)
    Input #ff, temp
    Debug.Print temp
Loop
Close #ff
0
 
VingamelAuthor Commented:
When trying to write the way you prescribe, I get a type mismatch error on
Write #ff, vA(iY);

I changed your code for my own project as follows...
Dim vA
Dim temp
Dim iY as Integer
Dim ff As Integer

For iY = 0 To 3599
    vA = MapTiles(3, 3599)
Next iY

ff = FreeFile
 
Open "JARNATH_SAVE" For Output As #ff   ' Open file for output.
       
   For iY = 0 To 3599
       Write #ff, vA(iY);
        Next iY
Close #ff
0
 
Éric MoreauSenior .Net ConsultantCommented:
To make an array with what you just read, use the SPLIT function.

vA = split(temp,",")
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
VingamelAuthor Commented:
vA = split(temp,",")?  How do my MapTiles(3,3599) array elements get into vA with that code?  Can you explain further?
0
 
VingamelAuthor Commented:
This does not work, emoreau...I am not certain how vA = Split(temp, ",") gets the elements of Maptiles(3, iY) into vA...  He'p me He'p me :o)

Dim vA
Dim temp
Dim ff As Integer
vA = Split(temp, ",")
For iY = 0 To 3599
    vA = MapTiles(3, iY)
Next iY


ff = FreeFile
   
Open "JARNATH_SAVE" For Output As #ff   ' Open file for output.
   For iY = 0 To 3599
     Write #ff, vA(iY);
   Next iY
0
 
VingamelAuthor Commented:
This does not work, emoreau...I am not certain how vA = Split(temp, ",") gets the elements of Maptiles(3, iY) into vA...  He'p me He'p me :o)

Dim vA
Dim temp
Dim ff As Integer
vA = Split(temp, ",")
For iY = 0 To 3599
    vA = MapTiles(3, iY)
Next iY


ff = FreeFile
   
Open "JARNATH_SAVE" For Output As #ff   ' Open file for output.
   For iY = 0 To 3599
     Write #ff, vA(iY);
   Next iY
0
 
VingamelAuthor Commented:
I am not rejecting any answer because it is wrong...just opening the question up for all to answer.  The answer provided does not work yet....
Thanks!
--Vingamel
0
 
Éric MoreauSenior .Net ConsultantCommented:
You said that your problem is to read?

Try something like this:
ff = FreeFile
Open "c:\test.dat" For Input As #ff
i = 0
Do While Not EOF(ff)
    Line Input #ff, temp
    MapTiles(3,i) = temp
    i = i + 1
Loop
Close #ff

0
 
VingamelAuthor Commented:
I am not rejecting any answer because it is wrong...just opening the question up for all to answer.  The answer provided does not work yet....
Thanks!
--Vingamel
0
 
kfrickCommented:
Try this:

Sub ReadMapFile()
Dim Index As Integer
Dim FileNum As Integer
Dim szTemp As String
Dim szChar As String * 1
    FileNum = FreeFile
    Index = 0   '(or -1, depending on your Option Base setting)
    Open "JARNATH_SAVE" For Input As #FileNum
    While Not EOF(FileNum)
        Input #FileNum, szChar 'read 1 character
        If szChar <> "," Then
            szTemp = szTemp + szChar
        Else
            Index = Index + 1
            MapTiles(3, Index) = szTemp
            szTemp = ""
        End If
    Wend
    Close #FileNum
End Sub


FYI:
If you change your output file routine to create a file in a list format rather that a Comma Delimited format, the read-back sub will be easier, because the Input# command will just read one record at a time. Then you won't have to test each character for a comma!

Change:
    Write #1, (stuff)
to:
    Print #1, (stuff)

then use:
Sub ReadMapFile()
Dim Index As Integer
Dim FileNum As Integer
Dim szTemp As String
    FileNum = FreeFile
    Open "JARNATH_SAVE" For Input As #FileNum
    For Index = 0 To 3599
        Input #FileNum, MapTiles(3, Index)
    Next
    Close #FileNum
End Sub

good luck!
-kf
0
 
VingamelAuthor Commented:
I am not rejecting any answer because it is wrong...just opening the question up for all to answer.  The answer provided does not work yet....
Thanks!
--Vingamel
0
 
VingamelAuthor Commented:
This worked!  Thank you, and to all others who helped.  Visit my site at www.jarnath.freeservers.com if you are interested in strategy games.  Here is what I did with emoreau's code, which populated maparray() with the correct information when it does the Input #:

Option Explicit
Dim maptiles(3, 3599)
Dim ff As Integer
Dim i As Integer
Dim temp
   

Private Sub Command1_Click()

For i = 0 To 3599
    maptiles(3, i) = 0
Next i
ff = FreeFile

Open "JARNATH_SAVE" For Output As #1   ' Open file for output.
               
        For i = 0 To 3599
            Write #1, maptiles(3, i),
        Next i
Close #ff

End Sub

Private Sub Command2_Click()
    Open "JARNATH_SAVE" For Input As #ff
i = 0
Do While Not EOF(ff)
    Line Input #ff, temp
    maptiles(3, i) = temp
    i = i + 1
Loop
Close #ff

End Sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now