Reputation: 6118
I have a big file and for the simplicity I am just showing a small part of it. The data looks like following:
NPSER NASER NQSER
10 5 3
TSSR MPSER JDNSR
15 10 6
What I need to do is to find for example NPSER and NASER and then assign the values NPSER as 10, NASER as 5 and NQSER as 3. For this small data set I could do as following:
TextReader infile = new StreamReader(fileName);
string line;
int NPSER, NASER, NQSER;
line = infile.ReadLine();
string[] words = line.Split('\t');
NPSER = Convert.ToInt32(words[0]);
NASER = Convert.ToInt32(words[1]);
NQSER = Convert.ToInt32(words[2]);
infile.Close();
Instead of reading each line and assigning values, I want to write a function which will automatically fetch the line when I search upto three words in a line which would be easier and efficient for longer application.
I would appreciate other methods as well.
Upvotes: 0
Views: 67
Reputation: 101731
It would be easier if you can use LINQ:
var line = File.ReadLines("path")
.SkipWhile(line => !line.Contains("NPSER")) // change this condition to suit your needs
.Skip(1)
.First();
var values = line.Split(new[] { ' '},StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse)
.ToArray();
int NPSER = values[0];
int NASER = values[1];
int NQSER = values[2];
Upvotes: 1