[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

Not holding previous line read from text file

This is what would be in the text file
Text File
May 3, 2005 12:48:50 PM
my message

What I have showing up is the line "my message" in all the messageboxes that would display the currentline and the previous line. What I need in the previous line is the date.


try
{
   if (File.Exists(sDirectory + sCurrentFileName))
   {
          if ((dCurrentErrorLogFileDate == dDayBefore && dHour >= dRunAtGMTHour ) ||
      (dCurrentErrorLogFileDate == dToday && dHour < dRunAtGMTHour))
          {
      bool bFound = false;

      //Get string to search for
      string sMsgLookingFor = "my message";
                                          
      //open the file for reading
      StreamReader objReader = new StreamReader(sDirectory + sCurrentFileName);
      
      string sCurrentLine = objReader.ReadLine();
      string sPreviousLine;

                string searchThis = sMsgLookingFor;

                while(sCurrentLine != null && sCurrentLine != searchThis)
      {
               //get the line before date and time value for comparison
                sPreviousLine = sCurrentLine;

                        if(sCurrentLine.IndexOf(searchThis) > -1)
              {
            bFound = true;
            
                          System.Diagnostics.EventLog.WriteEntry(this.ToString(), sDateHappend + " DateHappened");

                 MessageBox.Show(sCurrentLine, "InSearchthis", MessageBoxButtons.OK,
                                MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                                MessageBoxOptions.ServiceNotification);

                           MessageBox.Show(sPreviousLine, "InSearchthisPerviousLine", MessageBoxButtons.OK,
            MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
            MessageBoxOptions.ServiceNotification);
                                          
                 if (bFound)
                 {
                                 System.Diagnostics.EventLog.WriteEntry("Test", "Found");
            MessageBox.Show(sCurrentLine, "FollowFoundError", MessageBoxButtons.OK,
                       MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                       MessageBoxOptions.ServiceNotification);

            MessageBox.Show(sPreviousLine, "ErrorPreviousline", MessageBoxButtons.OK,
                       MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                        MessageBoxOptions.ServiceNotification
                             }
                 }
            sCurrentLine = objReader.ReadLine();
            }
                objReader.Close();
            }
       }
 }

 catch (Exception e)
{
System.Diagnostics.EventLog.WriteEntry(this.ToString(),
ERROR.FAILED_TO_READ_ERROR_LOG + e.ToString(),
System.Diagnostics.EventLogEntryType.Error);
}

}
0
NewMom2Brandon
Asked:
NewMom2Brandon
  • 4
  • 2
1 Solution
 
TheMCSECommented:
Can you elaborate on the format of your text file?  It's not clear to me what the format is, or what exactly you are trying to retrieve.  Without copying and pasting this into something where the indentation looks a little more natural :), this sounds as if it could be a problem of variable scope.  If you display the variable at known locations, do they all return the same unexpected result?  Is your text file format date, followed by a load of messages, or alternating dates and messages?  Good luck!
0
 
Julian HansenCommented:
There are a few things I can see off the bat - the most obvious one however is the one that is causign the problem

while(sCurrentLine != null && sCurrentLine != searchThis)
{
      //get the line before date and time value for comparison
      sPreviousLine = sCurrentLine; <--- After this line sPreviousLine = sCurrentLine - so obviously they will be the same

You need to move the line above to the end of the loop.

Additional comment

you are setting bFound = true and then checking within the same block if it is true - it will always evaluate true
secondly you are never setting bFound to False again - maybe this is what you want tho.
0
 
Julian HansenCommented:
>> You need to move the line above to the end of the loop.

In other words move it to ...

...
  sPreviousLine = sCurrentLine;  <---- here ...
  sCurrentLine = objReader.ReadLine();
  }
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
NewMom2BrandonAuthor Commented:
when I move sPrevious line to where you are mentioning....

then I get a use of unassigned local variable error for the messageboxes inside the if statements.
0
 
Julian HansenCommented:
I have just seen another potential problem

Your while loop says

while(sCurrentLine != null && sCurrentLine != searchThis)

if searchThis is exactly equal to sCurrentLine (i.e. you are doing a full string rather than a substring search) the loop will terminate before you get to the logic where you can dispaly the results.

Rather do this

string sCurrentLine = objReader.ReadLine();
string sPreviousLine =  ;

while (sCurrentLine != NULL )
{
    if ( sCurrentLine.IndexOf(searchThis) > -1 )
    {
       if ( sPreviousLine != NULL )
       {
          MessageBox.Show ....
          // optionally break the loop at this point
       }
    }

   sPreviousLine = sCurrentLine
    sCurentLine = objReader.ReadLine() ;
}

etc

0
 
NewMom2BrandonAuthor Commented:
YAAAA it worked...Thank you so much!!!
0
 
Julian HansenCommented:
You are most welcome - glad to have been of assistence
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now