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

Writing and reading arrays to file

I am pretty new to visual basic. I have a program where i am making a two dimensional array, and i want to write it to a file and then later read it from a file to use in the program.  how do i write the array to a file (it is just a 2-dimensional array of strings) and then later read it back in.  also, how can i use a file box to choose what file i want to save it to, so more than one file can be used in the program?  thanks

.pb
0
philburger
Asked:
philburger
  • 2
  • 2
  • 2
  • +2
1 Solution
 
EsopoCommented:
You want a CommonDialog control for choosing the file.

Put a textbox, a button and a CommonDialog in your form.

Add this code to your button:

CommonDialog1.showopen
Text1.Text = CommonDialog1.Filename

You should get the picture.

***About the array:***

To keep it simple, I suggest you save to a text file, with something like this:

***************
open "file.txt" for output as #1
   for j=0 to ubound(Array1)
      print #1, Array1(j,1)
      print #1, Array1(j,2)
   next j
close #1
***************

This will put your two dimensional array (Array1 in this case) into a text file, to read from the file you could use something like this:

***************
open "file.txt" for input as #1
   for j=0 to ubound(Array1)
      line input #1, Array1(j,1)
      line input #1, Array1(j,2)
   next j
close #1
***************


... you get the point. If you have trouble with any of this or are not familiar with the code, you should check the documentation.

I'm not sure about the syntax, hope this works.

Best regards.
0
 
jangeadorCommented:
homework questions should be worth more points
0
 
philburgerAuthor Commented:
thanks, this was not a homework question. i was really just wondeirng.  but what is ubound? i understand what it SHOULD do, but it did not.   weird.......
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!

 
EsopoCommented:
Ubound is used to retreive the last position of the array, of course, if you know the end of it (wich you should) you could just use that number instead.

Regards.
0
 
jangeadorCommented:
For best programming practice use

LBound(Array1) To UBound(Array1)
0
 
philburgerAuthor Commented:
thanks, i got it to work......i think
0
 
CleanupPingCommented:
philburger:
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
 
DanRollinsCommented:
Moderator, my recommended disposition is:

    Accept Esopo's comment(s) as an answer.

DanRollins -- EE database cleanup volunteer
0

Featured Post

Industry Leaders: 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!

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