ReadLine and Random Access

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Danny Smith

    #1

    ReadLine and Random Access

    Hi,

    I need to read a file and be able to:

    1. Find the current position in the stream
    2. Have access to a handy ReadLine() method.

    Obviously the FileStream class supports random access, so you have a Seek()
    method and a Position property to find the current stream position, and the
    StreamReader class has a ReadLine() method. I thought using these together
    would give me what I needed:

    StreamReader sr = new StreamReader( File.OpenRead( filename ) );
    string line = sr.ReadLine();
    long position = sr.BaseStream.P osition;

    However, this does not work as the StreamReader has buffering which means
    the moment a Read call is made, much more than the single line is read from
    the BaseStream. Consequently the Position property does not return the
    position *immediately* after the end of the line, which is what I need.

    This is possible in Java with the RandomAccessFil e class, which supports
    methods for reading lines and seeking / obtaining the position. Is there
    are way to do this in C#?

    One option would be to write my own ReadLine method which would allow me to
    keep track of the stream position, but I'd like to know if there is a way to
    avoid this.

    Can anyone help?

    Danny Smith


  • Tu-Thach

    #2
    RE: ReadLine and Random Access

    Danny
    Since you are reading a line of text, you can keep track of how many characters you read. This will give you the position that you need. I haven't tried this myself, but it's worth a shot

    Tu-Thac


    ----- Danny Smith wrote: ----

    Hi

    I need to read a file and be able to

    1. Find the current position in the strea
    2. Have access to a handy ReadLine() method

    Obviously the FileStream class supports random access, so you have a Seek(
    method and a Position property to find the current stream position, and th
    StreamReader class has a ReadLine() method. I thought using these togethe
    would give me what I needed

    StreamReader sr = new StreamReader( File.OpenRead( filename ) )
    string line = sr.ReadLine()
    long position = sr.BaseStream.P osition

    However, this does not work as the StreamReader has buffering which mean
    the moment a Read call is made, much more than the single line is read fro
    the BaseStream. Consequently the Position property does not return th
    position *immediately* after the end of the line, which is what I need

    This is possible in Java with the RandomAccessFil e class, which support
    methods for reading lines and seeking / obtaining the position. Is ther
    are way to do this in C#

    One option would be to write my own ReadLine method which would allow me t
    keep track of the stream position, but I'd like to know if there is a way t
    avoid this

    Can anyone help

    Danny Smit



    Comment

    • Scott

      #3
      Re: ReadLine and Random Access

      Hi Danny,

      I've created a class that parses a file of records that may handle your
      need. It maintains a file position, gives you random access to the records,
      and maintains a record count. Can you tell me more about what you're doing
      with the file? Are you breaking each line into fields? Can I get a sample of
      your file?

      Scott
      scott_ctr_@_mod o_mail.com_ remove all the underscores to email me.
      [color=blue]
      > I need to read a file and be able to:
      >
      > 1. Find the current position in the stream
      > 2. Have access to a handy ReadLine() method.[/color]


      Comment

      • Danny Smith

        #4
        Re: ReadLine and Random Access

        The only problem with that is that you don't know how many new-line
        characters there are. Either one or two, depending on the file format, but
        ReadLine() just returns the line and not these characters.

        Danny

        "Tu-Thach" <anonymous@disc ussions.microso ft.com> wrote in message
        news:06F05ED4-6F48-421C-B145-9D8831A3B645@mi crosoft.com...[color=blue]
        > Danny,
        > Since you are reading a line of text, you can keep track of how many[/color]
        characters you read. This will give you the position that you need. I
        haven't tried this myself, but it's worth a shot.[color=blue]
        >
        > Tu-Thach
        > www.ongtech.com
        >
        > ----- Danny Smith wrote: -----
        >
        > Hi,
        >
        > I need to read a file and be able to:
        >
        > 1. Find the current position in the stream
        > 2. Have access to a handy ReadLine() method.
        >
        > Obviously the FileStream class supports random access, so you have a[/color]
        Seek()[color=blue]
        > method and a Position property to find the current stream position,[/color]
        and the[color=blue]
        > StreamReader class has a ReadLine() method. I thought using these[/color]
        together[color=blue]
        > would give me what I needed:
        >
        > StreamReader sr = new StreamReader( File.OpenRead( filename ) );
        > string line = sr.ReadLine();
        > long position = sr.BaseStream.P osition;
        >
        > However, this does not work as the StreamReader has buffering which[/color]
        means[color=blue]
        > the moment a Read call is made, much more than the single line is[/color]
        read from[color=blue]
        > the BaseStream. Consequently the Position property does not return[/color]
        the[color=blue]
        > position *immediately* after the end of the line, which is what I[/color]
        need.[color=blue]
        >
        > This is possible in Java with the RandomAccessFil e class, which[/color]
        supports[color=blue]
        > methods for reading lines and seeking / obtaining the position. Is[/color]
        there[color=blue]
        > are way to do this in C#?
        >
        > One option would be to write my own ReadLine method which would allow[/color]
        me to[color=blue]
        > keep track of the stream position, but I'd like to know if there is a[/color]
        way to[color=blue]
        > avoid this.
        >
        > Can anyone help?
        >
        > Danny Smith
        >
        >
        >[/color]


        Comment

        • Danny Smith

          #5
          Re: ReadLine and Random Access

          Scott,

          The file is a pdf file. Some of the file needs to be read as bytes as it is
          not text, yet certain parts are more conveniently read as lines of text. I
          would like to be able to:

          1. Jump to a known position in the stream, position X
          2. Read a line of text
          3. Read some bytes.
          4. Jump a few bytes back in the stream, position X + y.

          The only unknown in this is the number new-line characters the line of text
          has, but this means that I cannot work out the value of y.

          Possibly I could read the line as bytes, then I would know exactly how many
          bytes it was. I just wondered if there was a slightly easier way, as after
          reading a line in Java you could query the stream for the exact position, so
          I thought there may be something I'm unaware of in C#.

          This would also be handy if you were reading in lines of text from a large
          file (with an unknown number of lines) and you wanted to display a progress
          bar showing how much of the file had been read. If you could get the
          current position in the stream you could then calculate the percentage read
          using the stream length. How would you do this?

          Thanks,

          Danny


          "Scott" <me@me.com> wrote in message
          news:%23bakYy89 DHA.2168@TK2MSF TNGP12.phx.gbl. ..[color=blue]
          > Hi Danny,
          >
          > I've created a class that parses a file of records that may handle your
          > need. It maintains a file position, gives you random access to the[/color]
          records,[color=blue]
          > and maintains a record count. Can you tell me more about what you're doing
          > with the file? Are you breaking each line into fields? Can I get a sample[/color]
          of[color=blue]
          > your file?
          >
          > Scott
          > scott_ctr_@_mod o_mail.com_ remove all the underscores to email me.
          >[color=green]
          > > I need to read a file and be able to:
          > >
          > > 1. Find the current position in the stream
          > > 2. Have access to a handy ReadLine() method.[/color]
          >
          >[/color]


          Comment

          • Tu-Thach

            #6
            Re: ReadLine and Random Access

            You can get the length of the string you just read. Seek to that location, read 1 or 2 characters to determine whether it is \n or \r\n. Then, add the appropriate count to your length

            Tu-Thac


            ----- Danny Smith wrote: ----

            The only problem with that is that you don't know how many new-lin
            characters there are. Either one or two, depending on the file format, bu
            ReadLine() just returns the line and not these characters

            Dann

            "Tu-Thach" <anonymous@disc ussions.microso ft.com> wrote in messag
            news:06F05ED4-6F48-421C-B145-9D8831A3B645@mi crosoft.com..[color=blue]
            > Danny
            > Since you are reading a line of text, you can keep track of how man[/color]
            characters you read. This will give you the position that you need.
            haven't tried this myself, but it's worth a shot[color=blue][color=green]
            >> Tu-Thac[/color]
            > www.ongtech.co[color=green]
            >> ----- Danny Smith wrote: ----
            >> Hi
            >> I need to read a file and be able to
            >> 1. Find the current position in the strea[/color]
            > 2. Have access to a handy ReadLine() method[color=green]
            >> Obviously the FileStream class supports random access, so you have[/color][/color]
            Seek([color=blue]
            > method and a Position property to find the current stream position[/color]
            and th[color=blue]
            > StreamReader class has a ReadLine() method. I thought using thes[/color]
            togethe[color=blue]
            > would give me what I needed[color=green]
            >> StreamReader sr = new StreamReader( File.OpenRead( filename ) )[/color]
            > string line = sr.ReadLine()
            > long position = sr.BaseStream.P osition[color=green]
            >> However, this does not work as the StreamReader has buffering whic[/color][/color]
            mean[color=blue]
            > the moment a Read call is made, much more than the single line i[/color]
            read fro[color=blue]
            > the BaseStream. Consequently the Position property does not retur[/color]
            th[color=blue]
            > position *immediately* after the end of the line, which is what[/color]
            need[color=blue][color=green]
            >> This is possible in Java with the RandomAccessFil e class, whic[/color][/color]
            support[color=blue]
            > methods for reading lines and seeking / obtaining the position. I[/color]
            ther[color=blue]
            > are way to do this in C#[color=green]
            >> One option would be to write my own ReadLine method which would allo[/color][/color]
            me t[color=blue]
            > keep track of the stream position, but I'd like to know if there is[/color]
            way t[color=blue]
            > avoid this[color=green]
            >> Can anyone help
            >> Danny Smit[color=darkred]
            >>>[/color][/color][/color]

            Comment

            • Scott

              #7
              Re: ReadLine and Random Access

              Unfortunately, my parser isn't up to this task either. Since you're mixing
              binary and text, I think your best bet would be to derive your own class
              from FileStream that adds a ReadLine method. You can look at the
              implementation of StreamReader
              (http://www.123aspx.com/rotor/rotorsrc.aspx?rot=42055) to help with
              implementing the ReadLine method. This should be a fairly simple job.

              Scott
              [color=blue]
              > The file is a pdf file. Some of the file needs to be read as bytes as it[/color]
              is[color=blue]
              > not text, yet certain parts are more conveniently read as lines of text.[/color]
              I[color=blue]
              > would like to be able to:
              >
              > 1. Jump to a known position in the stream, position X
              > 2. Read a line of text
              > 3. Read some bytes.
              > 4. Jump a few bytes back in the stream, position X + y.
              >
              > The only unknown in this is the number new-line characters the line of[/color]
              text[color=blue]
              > has, but this means that I cannot work out the value of y.
              >
              > Possibly I could read the line as bytes, then I would know exactly how[/color]
              many[color=blue]
              > bytes it was. I just wondered if there was a slightly easier way, as[/color]
              after[color=blue]
              > reading a line in Java you could query the stream for the exact position,[/color]
              so[color=blue]
              > I thought there may be something I'm unaware of in C#.
              >
              > This would also be handy if you were reading in lines of text from a large
              > file (with an unknown number of lines) and you wanted to display a[/color]
              progress[color=blue]
              > bar showing how much of the file had been read. If you could get the
              > current position in the stream you could then calculate the percentage[/color]
              read[color=blue]
              > using the stream length. How would you do this?[/color]


              Comment

              • Scott

                #8
                Re: ReadLine and Random Access

                Unfortunately, my parser isn't up to this task either. Since you're mixing
                binary and text, I think your best bet would be to derive your own class
                from FileStream that adds a ReadLine method. You can look at the
                implementation of StreamReader
                (http://www.123aspx.com/rotor/rotorsrc.aspx?rot=42055) to help with
                implementing the ReadLine method. This should be a fairly simple job.

                Scott
                [color=blue]
                > The file is a pdf file. Some of the file needs to be read as bytes as it[/color]
                is[color=blue]
                > not text, yet certain parts are more conveniently read as lines of text.[/color]
                I[color=blue]
                > would like to be able to:
                >
                > 1. Jump to a known position in the stream, position X
                > 2. Read a line of text
                > 3. Read some bytes.
                > 4. Jump a few bytes back in the stream, position X + y.
                >
                > The only unknown in this is the number new-line characters the line of[/color]
                text[color=blue]
                > has, but this means that I cannot work out the value of y.
                >
                > Possibly I could read the line as bytes, then I would know exactly how[/color]
                many[color=blue]
                > bytes it was. I just wondered if there was a slightly easier way, as[/color]
                after[color=blue]
                > reading a line in Java you could query the stream for the exact position,[/color]
                so[color=blue]
                > I thought there may be something I'm unaware of in C#.
                >
                > This would also be handy if you were reading in lines of text from a large
                > file (with an unknown number of lines) and you wanted to display a[/color]
                progress[color=blue]
                > bar showing how much of the file had been read. If you could get the
                > current position in the stream you could then calculate the percentage[/color]
                read[color=blue]
                > using the stream length. How would you do this?[/color]


                Comment

                • Danny Smith

                  #9
                  Re: ReadLine and Random Access

                  Scott,

                  That's exactly what I will do then! Thanks very much for that link - I
                  would not have known where to find that code!

                  Danny


                  "Scott" <me@me.com> wrote in message
                  news:uAweIG$9DH A.548@TK2MSFTNG P11.phx.gbl...[color=blue]
                  > Unfortunately, my parser isn't up to this task either. Since you're mixing
                  > binary and text, I think your best bet would be to derive your own class
                  > from FileStream that adds a ReadLine method. You can look at the
                  > implementation of StreamReader
                  > (http://www.123aspx.com/rotor/rotorsrc.aspx?rot=42055) to help with
                  > implementing the ReadLine method. This should be a fairly simple job.
                  >
                  > Scott
                  >[color=green]
                  > > The file is a pdf file. Some of the file needs to be read as bytes as[/color][/color]
                  it[color=blue]
                  > is[color=green]
                  > > not text, yet certain parts are more conveniently read as lines of text.[/color]
                  > I[color=green]
                  > > would like to be able to:
                  > >
                  > > 1. Jump to a known position in the stream, position X
                  > > 2. Read a line of text
                  > > 3. Read some bytes.
                  > > 4. Jump a few bytes back in the stream, position X + y.
                  > >
                  > > The only unknown in this is the number new-line characters the line of[/color]
                  > text[color=green]
                  > > has, but this means that I cannot work out the value of y.
                  > >
                  > > Possibly I could read the line as bytes, then I would know exactly how[/color]
                  > many[color=green]
                  > > bytes it was. I just wondered if there was a slightly easier way, as[/color]
                  > after[color=green]
                  > > reading a line in Java you could query the stream for the exact[/color][/color]
                  position,[color=blue]
                  > so[color=green]
                  > > I thought there may be something I'm unaware of in C#.
                  > >
                  > > This would also be handy if you were reading in lines of text from a[/color][/color]
                  large[color=blue][color=green]
                  > > file (with an unknown number of lines) and you wanted to display a[/color]
                  > progress[color=green]
                  > > bar showing how much of the file had been read. If you could get the
                  > > current position in the stream you could then calculate the percentage[/color]
                  > read[color=green]
                  > > using the stream length. How would you do this?[/color]
                  >
                  >[/color]


                  Comment

                  Working...