I have an application that reads information from a text file and then categorizes them and puts them onto a Database. For one category, I need to check the line that comes right after the current line and look for a certain keyword?
How do i get to read this line? This should happen when the streamreader has the current line already open....
I'm using c# on VS2010.
Edit:
All of the code below is in a while (!sReader.EndOfStream) loop
string line = sReader.ReadLine(); //Note: this is used way above and lots of things are done before we come to this loop
for (int i = 0; i < filter_length; i++)
{
if (searchpattern_queries[i].IsMatch(line) == true)
{
logmessagtype = selected_queries[i];
//*Here i need to add a if condition to check if the type is "RESTARTS" and i need to get the next line to do more classification. I need to get that line only to classify the current one. So, I'd want it to be open independently *
hit = 1;
if (logmessagtype == "AL-UNDEF")
{
string alid = AlarmID_Search(line);
string query = "SELECT Severity from Alarms WHERE ALID like '" +alid +"'";
OleDbCommand cmdo = new OleDbCommand(query, conn);
OleDbDataReader reader;
reader = cmdo.ExecuteReader();
while (reader.Read())
{
if (reader.GetString(0).ToString() == null)
{ }
else
{
string severity = reader.GetString(0).ToString();
if (severity == "1")
//Keeps going on.....
Also, the .log files that are opened might go upto 50 Mb types... ! Which is why i dont really prefer reading all lines and keeping track!
Can you not just call
reader.ReadLine()
again? Or is the problem that you then need to use the line in the next iteration of the loop?If it's a reasonably small file, have you considered reading the whole file using
File.ReadAllLines()
? That would probably make it simpler, although obviously a little less clean in other ways, and more memory-hungry for large files.EDIT: Here's some code as an alternative:
Here is an idiom to process the current line you while having the next line already available:
This is basically a queue with a maximum of two items in it, or "one line read-ahead".
Edit: Simplified.
I am not really a file IO expert... but why not do something like this:
Before you start reading lines declare two variables.
Then while you are reading...
You could save the position of the stream then after calling ReadLine, seek back to that position. However this is pretty inefficient.
I would store the result of ReadLine into a "buffer", and when possible use that buffer as a source. When it is empty, use ReadLine.
Simply use
and process the file with a
for (int i = 0; i < lines.Length; i ++)
loop.For a big file, simply cache the 'previous line' or do an out-of-band ReadLine().