8 Replies - 8722 Views - Last Post: 23 March 2010 - 12:08 PM Rate Topic: -----

#1 amitchhajer  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 78
  • Joined: 30-January 10

StreamReader.Readline cannot check end of file

Posted 22 March 2010 - 04:17 AM

Working with file handling in c#
my streamreader.readline is working perfectly fine but is not ending anywhere and going to a infine loop

How to check for End Of File
i am posting part of code....i even tried

if (fs.Length == fs.Position)

            int i = 0;
            FileStream fs = new FileStream("c:\\amit\\test.txt", FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
            sr.BaseStream.Seek(0, SeekOrigin.Begin);
            string str = sr.ReadLine();
            
            while (str != "null")
            {
                //MessageBox.Show(Convert.ToString(fs.Length));
                //MessageBox.Show(Convert.ToString(fs.Position));
                
                //if (fs.Length == fs.Position)
                  //  break;
                try
                {
                    i++;
                    TextBox t = (TextBox)GenerateDynamicControlstb(i - 1);
                    Button b = (Button)GenerateDynamicControlsbt(i - 1);
                    TextBox t2 = (TextBox)GenerateDynamicControlstb2(i - 1);
                         
                        
                       
                    t2.Text = str;
                        

                        str = sr.ReadLine();
                        t.Text = str;
                                            
                    str = sr.ReadLine();
                }
                catch (Exception e1)
                {
                }


               


                
                
            }
            sr.Close();
            fs.Close();



        }


Is This A Good Question/Topic? 0
  • +

Replies To: StreamReader.Readline cannot check end of file

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: StreamReader.Readline cannot check end of file

Posted 22 March 2010 - 04:41 AM

your while loop should be this...

while (str != null)


Was This Post Helpful? 1
  • +
  • -

#3 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: StreamReader.Readline cannot check end of file

Posted 22 March 2010 - 04:56 AM

This is the way you should do it:

using (StreamReader reader = new StreamReader("myfile.txt"))
{
    string line = string.Empty;
    while ((line = reader.ReadLine()) != null)
    {
        Console.WriteLine(line);
    }
}



Hope this helps...
Was This Post Helpful? 1
  • +
  • -

#4 Vishu Sukhdev  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 150
  • Joined: 19-February 10

Re: StreamReader.Readline cannot check end of file

Posted 23 March 2010 - 05:38 AM

You can also use this
 while (sr.Peek() != -1)
 


Was This Post Helpful? 0
  • +
  • -

#5 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 528
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: StreamReader.Readline cannot check end of file

Posted 23 March 2010 - 10:56 AM

View PostVishu Sukhdev, on 23 March 2010 - 04:38 AM, said:

You can also use this
 while (sr.Peek() != -1)
 



Peek doesn't advance the position so you will still need a ReadLine in there.

What FlashM said is the correct way to do this:

using (StreamReader reader = new StreamReader("myfile.txt"))
{
    string line = string.Empty;
    while ((line = reader.ReadLine()) != null)
    {
        Console.WriteLine(line);
    }
}


Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: StreamReader.Readline cannot check end of file

Posted 23 March 2010 - 11:09 AM

You can use Peek like that

using (StreamReader reader = new StreamReader("myfile.txt"))
{
    string line = string.Empty;
    while (sr.Peek() != -1)
    {
        Console.WriteLine(line);
    }
}




That will accomplish the same thing as FlashM's code (Just an FYI, not trying to say anyone is wrong :) )
Was This Post Helpful? 0
  • +
  • -

#7 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 528
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: StreamReader.Readline cannot check end of file

Posted 23 March 2010 - 11:28 AM

View PostPsychoCoder, on 23 March 2010 - 10:09 AM, said:

You can use Peek like that

using (StreamReader reader = new StreamReader("myfile.txt"))
{
    string line = string.Empty;
    while (sr.Peek() != -1)
    {
        Console.WriteLine(line);
    }
}




That will accomplish the same thing as FlashM's code (Just an FYI, not trying to say anyone is wrong :) )


From the looks of it, I think it's just going to print string.Empty forever. Peek doesn't advance the pointer so every Peek() is always looking at the same exact thing. You use peek to ensure that something's there. When you know it's there, you use ReadLine to get it (and advance the position internally on the reader).

EDIT: To the point of why it's going to print string.Empty forever, line is assigned as string.Empty and then never anything else.

This post has been edited by MentalFloss: 23 March 2010 - 11:29 AM

Was This Post Helpful? 0
  • +
  • -

#8 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: StreamReader.Readline cannot check end of file

Posted 23 March 2010 - 12:01 PM

You're right (I didn't notice line was being set in that manner, I should have checked out the entire code first. I was just trying to point out that Peek can be used in this manner). This will work properly

using (StreamReader reader = new StreamReader("myfile.txt"))
{
    while (sr.Peek() != -1)
    {
        Console.WriteLine(reader.ReadLine());
    }
}



Was This Post Helpful? 0
  • +
  • -

#9 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 528
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: StreamReader.Readline cannot check end of file

Posted 23 March 2010 - 12:08 PM

View PostPsychoCoder, on 23 March 2010 - 11:01 AM, said:

You're right (I didn't notice line was being set in that manner, I should have checked out the entire code first. I was just trying to point out that Peek can be used in this manner). This will work properly

using (StreamReader reader = new StreamReader("myfile.txt"))
{
    while (sr.Peek() != -1)
    {
        Console.WriteLine(reader.ReadLine());
    }
}




That's because

Console.WriteLine(reader.ReadLine());



advances the position. You're right though. They now do the same thing. I'm just trying to explain that Peek alone is not going to achieve it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1