Solved

Writing an array to a text file and also writing it back to the array in VB 2005

Posted on 2006-11-25
5
253 Views
Last Modified: 2008-02-01
Hi Everyone,

I would like to write an array (8 values) to a text file, so save the values. I would then like to be able to read the values and populate the array with the values in the text value. Using the text file as a place to save the array values during different sessions if you will.

I would welcome any help pages, or advice on where I should look on how to acheive this.

Thanks,
Adam
0
Comment
Question by:eadam-uk
  • 2
  • 2
5 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18012136
What kind of Array are you dealing with?   Integer, String?...

If String, will there be any carriage returns in the values?
0
 
LVL 29

Accepted Solution

by:
Nightman earned 500 total points
ID: 18012169
Here's a very simple example:


  Private Sub BuildArrayAndTestReadWrite()
    Dim x(8) As String

    x(0) = "1"
    x(1) = "2"
    x(2) = "3"
    x(3) = "4"
    x(4) = "5"
    x(5) = "6"
    x(6) = "7"
    x(7) = "8"
    SaveArrayToFile(x)
    LoadArrayFromFile(x)


  End Sub

  Private Sub LoadArrayFromFile(ByRef x As Array)
    Dim sr As StreamReader
    Dim i As Integer
    sr = New StreamReader("SavedArray.txt")

    Do While sr.Peek() >= 0
      'This is an arbitrary size for this example.

      x(i) = sr.ReadLine()
      'The output will look odd, because
      'only five characters are read at a time.

      i = i + 1
    Loop
    sr.Close()

  End Sub

  Private Sub SaveArrayToFile(ByRef x As Array)
    Dim sw As StreamWriter = New StreamWriter("SavedArray.txt")
    Dim i As Integer
    For i = LBound(x) To UBound(x)
      sw.WriteLine(x(i))
    Next
    sw.Close()
  End Sub
0
 
LVL 29

Expert Comment

by:Nightman
ID: 18012173
You would have to use
Imports System.IO
in the class

Also - ignore the comments in the code (straight from the MSDN ;) - and adjusted slightly)
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18012407
You could do something like...

    Dim values() As String = {"a", "b", "c", "d", "e", "f", "g", "h"}

    ' To write the values...
    My.Computer.FileSystem.WriteAllText("C:\someFile.txt", String.Join("|", values), False)

    ' To read the values...
    values = My.Computer.FileSystem.ReadAllText("C:\someFile.txt").Split("|")

(given that "|" is not in the original strings...)
0
 

Author Comment

by:eadam-uk
ID: 18012906
Thanks nightman, worked a treat.

Sorry idle_mind I didnt see your final comment before I dished out the points, but thank you too!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now