C# End Of File
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: C# End Of File

  1. #1
    Join Date
    Sep 2009
    Posts
    53

    C# End Of File

    Hi, in C++ when I want to loop text in from a text file I use eof

    ie.


    while (!List.eof())
    {
    getline (List,line);
    _str1 = line;
    getline (List,line);
    _str2 = line;
    }


    In C# there isnt a eof which I know of. Any ideas?

    Thanks.

  2. #2
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: C# End Of File

    Code:
    using (TextReader tr = new StreamReader(@"C:\test\test.txt"))
    {
         while (tr.Peek() >= 0)
         {
              // read the line and do whatever with it
         }
    }
    Peek will return -1 when there is nothing else to read.

  3. #3
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: C# End Of File

    Code:
             using (StreamReader SR = new StreamReader("C:/myfile.txt")) {
                string line = null;
                while ((line = SR.ReadLine()) != null) {
                   //do something with the line
                }
             }

  4. #4
    Join Date
    Sep 2009
    Posts
    53

    Re: C# End Of File

    Quote Originally Posted by dannystommen View Post
    Code:
             using (StreamReader SR = new StreamReader("C:/myfile.txt")) {
                string line = null;
                while ((line = SR.ReadLine()) != null) {
                   //do something with the line
                }
             }
    Thanks, that isn't quite what would work with what I wanted, as I have data which I want to be collected in lines of 5, and then cycled again to get the next 5 etc etc.. This seems to give me a line and do something with it, and then a separate single other line. (I think). However, great piece of code for the future, Thank you!

    Quote Originally Posted by eclipsed4utoo View Post
    Code:
    using (TextReader tr = new StreamReader(@"C:\test\test.txt"))
    {
         while (tr.Peek() >= 0)
         {
              // read the line and do whatever with it
         }
    }
    Peek will return -1 when there is nothing else to read.

    This worked perfectly for what I needed.

    Thank you.










    Thanks Both

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center