0

In my M.U.G.E.N tournament program, I want to process the match results from the log file, that is created by the game. The log looks like this:

[Match 1]
totalmatches = 1
team1.1 = 
team2.1 = 
stage = stages/kowloon.def

[Match 1 Round 1]
winningteam = 1
timeleft = -1.00
p1.name = Knuckles the Echidna
p1.life = 269
p1.power = 0
p2.name = Shadow the Hedgehog
p2.life = 0
p2.power = 2684

[Match 1 Round 2]
winningteam = 2
timeleft = -1.00
p1.name = Knuckles the Echidna
p1.life = 0
p1.power = 1510
p2.name = Shadow the Hedgehog
p2.life = 586
p2.power = 2967

[Match 1 Round 3]
winningteam = 2
timeleft = -1.00
p1.name = Knuckles the Echidna
p1.life = 0
p1.power = 3000
p2.name = Shadow the Hedgehog
p2.life = 789
p2.power = 777

What I want is to process the last winningteam property to determine the result of the match. What is the most effective way to achieve this? (maybe with LINQ)

  • 2
    please provide "a way" that you already tried to use in order to achieve this. How are you reading the file? How do you get the different lines. – Gilad Green Dec 03 '17 at 19:48
  • 1
    Possible duplicate of [Reading/writing an INI file](https://stackoverflow.com/questions/217902/reading-writing-an-ini-file) – Slai Dec 03 '17 at 20:44

1 Answers1

0

You can use File.ReadLines that returns an enumeration of lines as IEnumerable<string>.

// string path contains file path and file name.
string line = File.ReadLines(path)
    .LastOrDefault(ln => ln.StartsWith("winningteam ="));

Now split the string at =, trim the second part and you have the team number as string

if (line != null) {
    string team = line.Split('=')[1].Trim();
    // With "winningteam = 2", [0] = "winningteam ", [1] = " 2"

    // Optionally convert it to a number
    int teamNo = Int32.Parse(team);

    ...
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188