9 Replies - 734 Views - Last Post: 05 May 2011 - 03:20 PM Rate Topic: -----

#1 Erber  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 08-September 10

Strange Problem with StreamReader

Posted 05 May 2011 - 10:34 AM

I have a code in which StreamReader has begun to act up.

I'm reading a text file, which starts something like this:

#Software: Microsoft Internet Information Services 6.0
#Version: 1.0
#Date: 2011-01-01 00:04:26


The code is searching for URLs within the document, all of which start with "http".

My problem is that the StreamReader is only reading the first line over and over... here is the code I am having problems with:

      string[] filePaths = Directory.GetFiles(@"C:\Inetpub\wwwroot\kitchenerMarket\data\", "*.log");
      //this is where the data is stored, and it will read .log files, the type of file all the data is
      //next line makes it run for every file

      foreach (string fName in filePaths)
      {
          //goes up each time a new URL is returned to the array full of all the URLs
          int urlCount = 0;
          using (StreamReader sr = new StreamReader(fName))
          {
              string line = sr.ReadLine();

              //int result =  lineLooker(line, "http");
              int result = line.IndexOf("http");

              if (result >= 0)
              {
                   //rest of the code
              }
          }
      }



I've looked at the variables in debugger, and it seems like it's reading the same line again and again, as the output is always #Software: Microsoft Internet Information Services 6.0. Any and all insight into this problem is welcome.

Is This A Good Question/Topic? 0
  • +

Replies To: Strange Problem with StreamReader

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 431
  • View blog
  • Posts: 1,452
  • Joined: 28-April 09

Re: Strange Problem with StreamReader

Posted 05 May 2011 - 10:43 AM

going to need to see the rest of the code inside here

        if (result >= 0)
        {
             //rest of the code
        }



Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 6440
  • View blog
  • Posts: 23,438
  • Joined: 12-June 08

Re: Strange Problem with StreamReader

Posted 05 May 2011 - 10:51 AM

Where's the loop that reads each line of a file? All I see is a loop for going through all the files, then a 'read line' which is getting your first line. You are not traversing the guts of the file. So toss a new loop that reads until the 'end of file' per each file or reads the whole file and parses it out.

What confounds me is this line "StreamReader has begun to act up. That would imply this code was working before (sans changes).
Was This Post Helpful? 2
  • +
  • -

#4 Erber  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 08-September 10

Re: Strange Problem with StreamReader

Posted 05 May 2011 - 10:59 AM

View PostNakor, on 05 May 2011 - 10:43 AM, said:

going to need to see the rest of the code inside here

        if (result >= 0)
        {
             //rest of the code
        }



The program doesn't ever reach the rest of the code, since indexOf("http") always returns -1, since it can't find http.

View Postmodi123_1, on 05 May 2011 - 10:51 AM, said:

Where's the loop that reads each line of a file? All I see is a loop for going through all the files, then a 'read line' which is getting your first line. You are not traversing the guts of the file. So toss a new loop that reads until the 'end of file' per each file or reads the whole file and parses it out.

What confounds me is this line "StreamReader has begun to act up. That would imply this code was working before (sans changes).


I guess "act up" wasn't the best choice of words...

I think it should be reading through the file. It get's the name of every file in the directory, then the foreach loop runs for every file in the directory, and the streamreader is reading fName, which is the name of the current file. That should read through them all, right? If not, could you kindly enlighten me on which part doesn't work or what is missing?

This post has been edited by Erber: 05 May 2011 - 10:59 AM

Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 6440
  • View blog
  • Posts: 23,438
  • Joined: 12-June 08

Re: Strange Problem with StreamReader

Posted 05 May 2011 - 11:02 AM

Investigate "readline" versus "read". You might find that illuminating.

Read:
http://msdn.microsof...y/ath1fht8.aspx

Readline:
http://msdn.microsof...r.readline.aspx
Was This Post Helpful? 1
  • +
  • -

#6 Erber  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 08-September 10

Re: Strange Problem with StreamReader

Posted 05 May 2011 - 11:28 AM

View Postmodi123_1, on 05 May 2011 - 11:02 AM, said:

Investigate "readline" versus "read". You might find that illuminating.

Read:
http://msdn.microsof...y/ath1fht8.aspx

Readline:
http://msdn.microsof...r.readline.aspx


I've actually looked for solutions like this already and tried implementing a few different codes with different uses of Read() and ReadLine(), and I've drawn myself into other dead ends. I've read both of those pages and they gave me some ideas, but all the ideas had failed.

One idea I had that came close was replacing string line = sr.ReadLine() this:

StringBuilder builder = new StringBuilder();
                    while (sr.Peek() >= 0 && sr.Peek() != 41)
                    {
                        builder.Append((char)sr.Read());
                    }
                    string line = builder.ToString();

                    builder.Clear();


But this seems somewhat cumbersome.

This post has been edited by Erber: 05 May 2011 - 11:29 AM

Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 6440
  • View blog
  • Posts: 23,438
  • Joined: 12-June 08

Re: Strange Problem with StreamReader

Posted 05 May 2011 - 11:33 AM

Oddly the examples in each of those help docs would provide you with a solution.

Where did you rip those lines of code out of?


If you use 'read' you get the whole document at once and will need to go through and parse out the data you want. If you use 'readline' you need to have it loop through the whole file until it can't loop any more.
Was This Post Helpful? 1
  • +
  • -

#8 Erber  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 08-September 10

Re: Strange Problem with StreamReader

Posted 05 May 2011 - 11:37 AM

View Postmodi123_1, on 05 May 2011 - 11:33 AM, said:

Oddly the examples in each of those help docs would provide you with a solution.

Where did you rip those lines of code out of?


If you use 'read' you get the whole document at once and will need to go through and parse out the data you want. If you use 'readline' you need to have it loop through the whole file until it can't loop any more.


Really? I'll take another look... Also the files I'm using are log files, I don't know if that changes anything or not, but the variables in the debug windows looked normal.

*edit* I used to have it reading until it got to the end of the document, but some of the documents are upwards of 300 MB so that didn't work too well.

*double edit* Another thing I tried was reading until it hit an H and then seeing if the next character was a T then another T and a P, but that code was falling apart and I decided using readline() would make much better code.

This post has been edited by Erber: 05 May 2011 - 11:47 AM

Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5667
  • View blog
  • Posts: 22,511
  • Joined: 23-August 08

Re: Strange Problem with StreamReader

Posted 05 May 2011 - 12:05 PM

Any reason you're not using the StartsWith() method of the String class?
Was This Post Helpful? 1
  • +
  • -

#10 german129  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 43
  • Joined: 03-September 10

Re: Strange Problem with StreamReader

Posted 05 May 2011 - 03:20 PM

View PostErber, on 05 May 2011 - 11:37 AM, said:

View Postmodi123_1, on 05 May 2011 - 11:33 AM, said:

Oddly the examples in each of those help docs would provide you with a solution.

Where did you rip those lines of code out of?


If you use 'read' you get the whole document at once and will need to go through and parse out the data you want. If you use 'readline' you need to have it loop through the whole file until it can't loop any more.


Really? I'll take another look... Also the files I'm using are log files, I don't know if that changes anything or not, but the variables in the debug windows looked normal.

*edit* I used to have it reading until it got to the end of the document, but some of the documents are upwards of 300 MB so that didn't work too well.

*double edit* Another thing I tried was reading until it hit an H and then seeing if the next character was a T then another T and a P, but that code was falling apart and I decided using readline() would make much better code.


There is a perfect example in the msdn readline site that shows you how to loop through a file until you reach the end of the file. I really dont know if i should spoon feed this to you when everyone else is obviously trying to let you come up with it on your own but here is my attempt. BTW i did not test this at all so it could be completely useless.

while (!sr.EndOfStream()) {
   string words[] = sr.ReadLine().Split();
   foreach (string word in words){
      if(word.StartsWith("http",StringComparison.OrdinalIgnoreCase)){
          // Do something
      }
   }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1