-2

I need to parse a text file with the following format:

and -0.436527 -0.515304 -0.002056 -0.227969 0.177528 0.201756...
with 0.101336 0.493859 -0.081095 -0.391502 -0.111579 0.388659...
voice -0.168610 0.413912 0.423446 0.484159 -0.546614 0.558571...

There may be 100 such trailing numbers. Now I need to search for a certain text, say voice and store the numbers in an array or any faster data structure and do some mathematics on the same. What will be the fastest way to achieve it? The text file may be 100+MB in size!

Thanks!

Jishan
  • 1,654
  • 4
  • 28
  • 62

1 Answers1

1

Try to use File.ReadLines and parse the results with LINQ:

double[] numbers = File.ReadLines(path)
                    .Where(line => line.Contains("voice"))
                    .SelectMany(line => line.Split())
                    .Where(str => str.All(c => Char.IsDigit(c) || c == '-' || c == '.'))
                    .Select(str => Double.Parse(str, CultureInfo.InvariantCulture))
                    .ToArray();
w.b
  • 11,026
  • 5
  • 30
  • 49
  • I get this error `Input string was not in a correct format.` at `Double.Parse(str, CultureInfo.InvariantCulture))` – Jishan Aug 22 '15 at 17:14