Matt Matt - 1 year ago 72 Question

How to transfer data from a text file into an array and back to the text file

So basically. I have to update stock levels in a text file so i've put all my information into a text file like so:

Stock level, Target level, Restock
10, 20, 5

etc and i need to be able to change let's say "10" By sending it into an array then saving it and sending it back to the text file. EG. i take "10" and do "10"(But as a variable) - Quantity..... Quantity being the amount the user orders. For this example lets say Quantity = 3 so then it will make "10" into "7" and save it in the text file as "7" so the next time i open it it will say "7" and not "10"? Any ideas would be much appreciated.

Answer Source

Like Plutonix said, this sort of thing is what databases were made for, and you would need some sort of identifier for each "row" of stocks...but if you absolutely must store the data as text, then you'd be much better off storing it as xml and serializing it. Here's an example of the concept, it's only using one "line", but more could be added. It's just to show you how you can save, reload, manipulate, and then resave the data:

    Imports System.IO
    Imports System.Xml.Serialization

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim filepath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.xml")
            Dim lstStocks As New List(Of Stock)
            lstStocks.Add(New Stock("stk1", 10, 20, 5))

            MsgBox(lstStocks(0).Name & vbCrLf &
                "Stock Level: " & lstStocks(0).StockLevel.ToString & vbCrLf &
                "Target Level: " & lstStocks(0).TargetLevel.ToString & vbCrLf &
                "Restock:" & lstStocks(0).Restock.ToString)

            Serialize(lstStocks, filepath)

            Dim firstLoadStocks As List(Of Stock) = Deserialize(filepath)
            firstLoadStocks(0).StockLevel -= 3
            Serialize(firstLoadStocks, filepath)

            Dim secondLoadStocks As List(Of Stock) = Deserialize(filepath)
            MsgBox(secondLoadStocks(0).Name & vbCrLf &
                "Stock Level: " & secondLoadStocks(0).StockLevel.ToString & vbCrLf &
                "Target Level: " & secondLoadStocks(0).TargetLevel.ToString & vbCrLf &
                "Restock:" & secondLoadStocks(0).Restock.ToString)

        End Sub

        Public Sub Serialize(ByVal ListOfStocks As List(Of Stock), ByVal fp As String)
            Using sw As New StreamWriter(fp)
                Dim xmlSer As New XmlSerializer(ListOfStocks.GetType)
                xmlSer.Serialize(sw, ListOfStocks)
            End Using
        End Sub

        Public Function Deserialize(ByVal fp As String) As List(Of Stock)
            Dim lstStocks As New List(Of Stock)
            Using sr As New StreamReader(fp)
                Dim xmlSer As New XmlSerializer(lstStocks.GetType)
                lstStocks = xmlSer.Deserialize(sr)
            End Using
            Return lstStocks
        End Function
    End Class
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download