StreamReader / ReadLine()


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: StreamReader / ReadLine()

  1. #1
    Ned Hoyek Guest

    StreamReader / ReadLine()

    I am having problem reading the line at the location set by seek. For
    example:

    StreamReader fin = new StreamReader("file.txt",System.Text.Encoding.ASCII);
    string Line;

    while( (Line=fin.ReadLine())!= null)
    {

    //
    // Some code here


    if(Line[0]=='$') {
    MessageBox.Show(fin.BaseStream.Position.ToString()); //displays 100
    fin.BaseStream.Seek(0,SeekOrigin.Begin);
    MessageBox.Show(fin.BaseStream.Position.ToString()); //displays 0
    break; }

    }

    MessageBox.Show(fin.BaseStream.ReadByte()); //Reads the 1st byte in the file
    //However
    MessageBox.Show(fin.ReadLine()); //Reads the Line at position 100 and not 0


    How can I get the ReadLine() method to read the line at the position set by
    Seek()?

    Any help would be appreciated.
    Thank you
    Ned



  2. #2
    Russell Jones Guest

    Re: StreamReader / ReadLine()

    Changing the BaseStream position doesn't change the position of the
    StreamReader. I'm not sure what you're trying to accomplish by manipulating
    the BaseStream in your example. If you're trying to "skip" a certain number
    of characters, use the StreamReader.ReadBlock method. That reads the
    characters and updates the StreamReader position appropriately. If you're
    just trying to strip some characters off the front of each line that you
    read, read the line and then use the SubString method to strip the
    characters.


    "Ned Hoyek" <nhoyek@indsci.com> wrote in message
    news:3e36b49d$1@tnews.web.devx.com...
    > I am having problem reading the line at the location set by seek. For
    > example:
    >
    > StreamReader fin = new

    StreamReader("file.txt",System.Text.Encoding.ASCII);
    > string Line;
    >
    > while( (Line=fin.ReadLine())!= null)
    > {
    >
    > //
    > // Some code here
    >
    >
    > if(Line[0]=='$') {
    > MessageBox.Show(fin.BaseStream.Position.ToString()); //displays 100
    > fin.BaseStream.Seek(0,SeekOrigin.Begin);
    > MessageBox.Show(fin.BaseStream.Position.ToString()); //displays 0
    > break; }
    >
    > }
    >
    > MessageBox.Show(fin.BaseStream.ReadByte()); //Reads the 1st byte in the

    file
    > //However
    > MessageBox.Show(fin.ReadLine()); //Reads the Line at position 100 and not

    0
    >
    >
    > How can I get the ReadLine() method to read the line at the position set

    by
    > Seek()?
    >
    > Any help would be appreciated.
    > Thank you
    > Ned
    >
    >




  3. #3
    Ned Hoyek Guest

    Re: StreamReader / ReadLine()

    Hi Russ,

    I can always read the file into one string and manipulate it but I prefer to
    read one line at a time if I can.
    Is there a way to change the position of a StreamReader, in a way that if I
    call ReadLine() afterwards it will start reading from the new position?

    Thank you
    Ned





    "Russell Jones" <arj1@nospam.northstate.net> wrote in message
    news:3e36d0e2@tnews.web.devx.com...
    > Changing the BaseStream position doesn't change the position of the
    > StreamReader. I'm not sure what you're trying to accomplish by

    manipulating
    > the BaseStream in your example. If you're trying to "skip" a certain

    number
    > of characters, use the StreamReader.ReadBlock method. That reads the
    > characters and updates the StreamReader position appropriately. If you're
    > just trying to strip some characters off the front of each line that you
    > read, read the line and then use the SubString method to strip the
    > characters.
    >
    >
    > "Ned Hoyek" <nhoyek@indsci.com> wrote in message
    > news:3e36b49d$1@tnews.web.devx.com...
    > > I am having problem reading the line at the location set by seek. For
    > > example:
    > >
    > > StreamReader fin = new

    > StreamReader("file.txt",System.Text.Encoding.ASCII);
    > > string Line;
    > >
    > > while( (Line=fin.ReadLine())!= null)
    > > {
    > >
    > > //
    > > // Some code here
    > >
    > >
    > > if(Line[0]=='$') {
    > > MessageBox.Show(fin.BaseStream.Position.ToString()); //displays 100
    > > fin.BaseStream.Seek(0,SeekOrigin.Begin);
    > > MessageBox.Show(fin.BaseStream.Position.ToString()); //displays 0
    > > break; }
    > >
    > > }
    > >
    > > MessageBox.Show(fin.BaseStream.ReadByte()); //Reads the 1st byte in the

    > file
    > > //However
    > > MessageBox.Show(fin.ReadLine()); //Reads the Line at position 100 and

    not
    > 0
    > >
    > >
    > > How can I get the ReadLine() method to read the line at the position set

    > by
    > > Seek()?
    > >
    > > Any help would be appreciated.
    > > Thank you
    > > Ned
    > >
    > >

    >
    >




  4. #4
    Russell Jones Guest

    Re: StreamReader / ReadLine()

    "Ned Hoyek" <nhoyek@indsci.com> wrote in message
    news:3e37008c$1@tnews.web.devx.com...
    > Hi Russ,
    >
    > I can always read the file into one string and manipulate it but I prefer

    to
    > read one line at a time if I can.
    > Is there a way to change the position of a StreamReader, in a way that if

    I
    > call ReadLine() afterwards it will start reading from the new position?
    >
    > Thank you
    > Ned


    Ned:

    Yes, in my last post I mentioned that you can skip over characters with a
    StreamReader without advancing to the next line by using the ReadBlock
    method. I don't know exactly what you're doing, but it seems that it would
    be much simpler for you to simply ignore the lines containing content you
    don't want. That doesn't mean that you have to read the entire file into a
    string, and doesn't prevent you from using ReadLine().

    If you provide a better explanation of what exactly you're trying to
    accomplish, someone here will help.

    Thanks,
    Russell Jones,
    Executive Editor,
    DevX.com



  5. #5
    Ned Hoyek Guest

    Re: StreamReader / ReadLine()

    Russ,

    Thanks for your time, ReadBlock() worked fine for me as you suggested.

    The question in my last post
    >Is there a way to change the position of a StreamReader, in a way that if I
    > > call ReadLine() afterwards it will start reading from the new position?

    is more like a general question now. I guess the answer to that is No,
    ReadLine() will not work with seek().

    Thank you
    Ned







    "Russell Jones" <arj1@nospam.northstate.net> wrote in message
    news:3e370c45@tnews.web.devx.com...
    > "Ned Hoyek" <nhoyek@indsci.com> wrote in message
    > news:3e37008c$1@tnews.web.devx.com...
    > > Hi Russ,
    > >
    > > I can always read the file into one string and manipulate it but I

    prefer
    > to
    > > read one line at a time if I can.
    > > Is there a way to change the position of a StreamReader, in a way that

    if
    > I
    > > call ReadLine() afterwards it will start reading from the new position?
    > >
    > > Thank you
    > > Ned

    >
    > Ned:
    >
    > Yes, in my last post I mentioned that you can skip over characters with a
    > StreamReader without advancing to the next line by using the ReadBlock
    > method. I don't know exactly what you're doing, but it seems that it would
    > be much simpler for you to simply ignore the lines containing content you
    > don't want. That doesn't mean that you have to read the entire file into a
    > string, and doesn't prevent you from using ReadLine().
    >
    > If you provide a better explanation of what exactly you're trying to
    > accomplish, someone here will help.
    >
    > Thanks,
    > Russell Jones,
    > Executive Editor,
    > DevX.com
    >
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center