Michael Schilling
Michael Schilling

Reputation: 360

Change just one line in a text file?

I have a text file with the format:

(title,price,id#)

CD1,11.00,111111
CD2,12.00,222222
CD3,13.00,333333
CD4,14.00,444444
CD5,15.00,555555
CD6,16.00,666666

What is the best way to go change the price of the appropriate CD if I'm given the id# and new price?

I'm sure it has something do to with getting the line and splitting it, but I'm not sure how I edit just one line and not mess up the whole file.

Upvotes: 1

Views: 11961

Answers (3)

Alex
Alex

Reputation: 8116

If its just a file with like 25 lines, you could do a simple input-transform-output routine and update the price per line.

Something like this (Using Streamreader / writer ).

Sub UpdatePrice(ByVal pricesToUpdate As Dictionary(Of Integer, String), ByVal inputPath As String)
    If Not IO.File.Exists(inputPath) Then Return
    Try
        Using inputStream = New IO.StreamReader(inputPath, System.Text.Encoding.UTF8, True)
            Using outputStream = New IO.StreamWriter(inputPath + ".tmp", False, System.Text.Encoding.UTF8)
                While Not inputStream.EndOfStream
                    Dim inputLine = inputStream.ReadLine
                    Dim content = inputLine.Split(","c)
                    If Not content.Length >= 3 Then
                        outputStream.WriteLine(inputLine)
                        Continue While
                    End If
                    Dim id As Integer
                    If Not Integer.TryParse(content(2), id) Then
                        outputStream.WriteLine(inputLine)
                        Continue While
                    End If
                    If Not pricesToUpdate.ContainsKey(id) Then
                        outputStream.WriteLine(inputLine)
                        Continue While
                    End If
                    content(1) = pricesToUpdate(id)
                    outputStream.WriteLine(String.Join(",", {content(0), content(1), content(2)}))
                End While
            End Using
        End Using
        If IO.File.Exists(inputPath + ".tmp") Then
            IO.File.Delete(inputPath)
            IO.File.Move(inputPath + ".tmp", inputPath)
        End If
    Catch ex As IO.IOException
        If IO.File.Exists(inputPath + ".tmp") Then IO.File.Delete(inputPath + ".tmp")
    End Try
End Sub

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460018

You can't rewrite a line without rewriting the entire file (unless the lines happen to be the same length). For such a small file it's probably the easiest to change the line in memory and then rewrite all to the file:

Dim idToFind = "444444"
Dim newPrice = "100"
Dim lines = IO.File.ReadAllLines(path)
For i = 0 To lines.Length - 1
    Dim line = lines(i)
    Dim fields = line.Split(","c)
    If fields.Length > 2 Then
        Dim id = fields(2)
        If id = idToFind Then
            Dim title = fields(0)
            lines(i) = String.Format("{0},{1},{2}", title, newPrice, id)
            Exit For
        End If
    End If
Next
IO.File.WriteAllLInes(path, lines)

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1499770

Okay, now we know it's a short file, life becomes much easier:

  • Load the file into an array of lines using File.ReadAllLines
  • Find the right line using string.Split to split each line into the constituent parts, and check the ID.
  • When you've found the right line, replace it with the complete new line
  • Write the file back with File.WriteAllLines

That should be enough to get you going.

Upvotes: 2

Related Questions