0

I'm creating a software which is supposed to read a text file and display only certain lines in which a certain string occurs. I will attach the file I am using:

[fltsim.0]
title=Boeing 737-800 Paint1
sim=Boeing737-800
model=
panel=
sound=
texture=1
kb_checklists=Boeing737-800_check
kb_reference=Boeing737-800_ref
atc_id=N737W
atc_airline=Boeing
atc_flight_number=
ui_manufacturer="Boeing"
ui_type="737-800"
ui_variation="Boeing livery"
ui_typerole="Commercial Airliner"
ui_createdby="Microsoft Corporation"
description="One should hardly be surprised that the world's most prolific manufacturer of commercial aircraft is also the producer of the world's most popular jetliner. The 737 became the best-selling commercial jetliner worldwide when orders for it hit 1,831 in June 1987 (surpassing Boeing's own 727 as the previous champ). However, it wasn't always that way\s in the first few years of production, there were so few orders that Boeing considered canceling the program. They didn't, and the airplane has more than proven itself in over three decades of service."

[fltsim.1]
title=Boeing 737-800 Paint2
sim=Boeing737-800
model=
panel=
sound=
texture=2
kb_checklists=Boeing737-800_check
kb_reference=Boeing737-800_ref
atc_id=N737X
atc_airline=World Travel
atc_flight_number=
ui_manufacturer="Boeing"
ui_type="737-800"
ui_variation="World Travel Airlines"
ui_typerole="Commercial Airliner"
ui_createdby="Microsoft Corporation"
description="One should hardly be surprised that the world's most prolific manufacturer of commercial aircraft is also the producer of the world's most popular jetliner. The 737 became the best-selling commercial jetliner worldwide when orders for it hit 1,831 in June 1987 (surpassing Boeing's own 727 as the previous champ). However, it wasn't always that way\s in the first few years of production, there were so few orders that Boeing considered canceling the program. They didn't, and the airplane has more than proven itself in over three decades of service."

As you can see, the header, [fltsim.0,1,etc.] increments by one every time, and underneath each header is a host of information that I need to pull out. I wish to pull out title={string}. I was able to pull out the first occurrence of title, however I cannot seem to figure out how to pull out the rest. All in all, my objective is to read specific data from that file (with more than one occurance) and also write to the file the same way. Example: If I wanted to pull out title={string}, I would want to get Boeing 737-800 Paint 1, and Boeing 737-800 Paint 2 (they could be named anything). Here is the code I was working with (only found first occurrence):

Dim path As String = "C:\Aircraft.cfg" '<<<<<CHANGE THIS LATER

        'GET LINE HUMBER OF STRING
        Dim firstNumber As Integer
        Dim toSearch = "ui_variation="
        Dim lineNumber = File.ReadLines(path).
                 Where(Function(l) l.Contains(toSearch))
                 Select(Function(l, index) index)    
        If lineNumber.Any Then
            firstNumber = lineNumber.First
        End If    
        'READS WHAT IS IN THE LINE WITH LINE NUMBER
        Using file As New StreamReader(path)
            For i As Integer = 1 To firstNumber + 1
                If file.ReadLine() Is Nothing Then
                    Throw New ArgumentOutOfRangeException("lineNumber")
                End If
            Next
            Dim line As String = file.ReadLine()
            If line Is Nothing Then
                Throw New ArgumentOutOfRangeException("lineNumber")
            End If
            MsgBox(line)
        End Using
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Sid
  • 17
  • 4

1 Answers1

0

Your file looks like an ini file, so there are some options for reading that format. However, to get all values for a particular key, your code is almost there. Instead of selecting the indices for the matching lines and rereading the file, you can just select the values:

Dim values As IEnumerable(Of String) =
    File.ReadLines(path).
        Where(Function(l) l.Contains(toSearch)).
        Select(Function(l) l.Split("="c)(1))
For Each value In values
    MsgBox(value)
Next

UPDATE

Here's one possible (although not particularly efficient) way to update the data:

' Note: ReadAllLines to read the complete file into memory
Dim lines = File.ReadAllLines(path)
For i = 0 To lines.Count - 1
    If lines(i).Contains(toSearch) Then
        lines(i) &= " - updated"
        ' or perhaps something like
        ' lines(i) = toSearch & "New Value"
    End If
Next
File.WriteAllLines(path, lines)
Community
  • 1
  • 1
Mark
  • 8,140
  • 1
  • 14
  • 29
  • Ok this worked perfectly! Thanks so much. Would I be able to use the same format to write to the file the same way (aka change the value within `title=`) – Sid Oct 06 '15 at 22:29
  • @Sid LINQ is not a great option for updating data. Using the INI file API I linked to would work better (although you would have to loop to check for how many sections exist). You could also just read all the data, loop through it to make updates, then write out the file again (assuming the files comfortably fit in memory). I'll add an example of that. For large files, perhaps the last option in [this answer](http://stackoverflow.com/a/6394534/2278086) would be more useful. – Mark Oct 07 '15 at 17:56