473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.Position;

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 RandomAccessFile 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
Nov 15 '05 #1
8 6764
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
www.ongtech.co

----- 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.Position

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 RandomAccessFile 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

Nov 15 '05 #2
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_@_modo_mail.com_ remove all the underscores to email me.
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.

Nov 15 '05 #3
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" <an*******@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...
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-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 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.Position;

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 RandomAccessFile 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

Nov 15 '05 #4
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:%2****************@TK2MSFTNGP12.phx.gbl...
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_@_modo_mail.com_ remove all the underscores to email me.
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.


Nov 15 '05 #5
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
www.ongtech.co

----- 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" <an*******@discussions.microsoft.com> wrote in messag
news:06**********************************@microsof t.com..
Danny
Since you are reading a line of text, you can keep track of how man characters you read. This will give you the position that you need.
haven't tried this myself, but it's worth a shot
Tu-Thac

www.ongtech.co
----- 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 Seek( method and a Position property to find the current stream position and th StreamReader class has a ReadLine() method. I thought using thes togethe would give me what I needed
StreamReader sr = new StreamReader( File.OpenRead( filename ) ) string line = sr.ReadLine()
long position = sr.BaseStream.Position
However, this does not work as the StreamReader has buffering whic

mean the moment a Read call is made, much more than the single line i read fro the BaseStream. Consequently the Position property does not retur th position *immediately* after the end of the line, which is what need This is possible in Java with the RandomAccessFile class, whic

support methods for reading lines and seeking / obtaining the position. I ther are way to do this in C#
One option would be to write my own ReadLine method which would allo
me t keep track of the stream position, but I'd like to know if there is way t avoid this
Can anyone help
Danny Smit

Nov 15 '05 #6
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
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?

Nov 15 '05 #7
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
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?

Nov 15 '05 #8
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:uA*************@TK2MSFTNGP11.phx.gbl...
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
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?


Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Brano Zarnovican | last post by:
Hi ! I'd like to init curses and still have working Python interactive command line. I found that you can replace stdin/stdout/stderr like this: #!/usr/bin/python -i import curses import...
1
by: Jian Qiu | last post by:
Hi, I tried to install python2.4.2. Unfortunately building 'readline' extension failed. Here is what I got: (It is a bit long. If you are impatient, please look at the end where it reports the...
6
by: Christian Convey | last post by:
Hello, I've got a program that (ideally) perpetually monitors sys.stdin for lines of text. As soon as a line comes in, my program takes some action. The problem is, it seems like a very large...
2
by: Piotr | last post by:
Can I create a Random Access Iterator which start at a certain index and end at a certain index of a container? I go thru this page, but I can't find an example....
3
by: Bruce | last post by:
I am building a WinForms app that uses Web Services access to a server for most of its data input/output, but I also need to persist some of its data to the local disk (basically as a cache of some...
6
by: InnoCreate | last post by:
Hi everyone. I've recently written a classic asp website which uses an MS Access datasource. I know this is less than an ideal data source as it has limited functionality. I have a search form on...
25
by: lnatz | last post by:
Hi, I am writing a shell(project). I would like to use readline, ncurses and/or history to search history. I want to write a function that would recognize the KEY_UP and KEY_DOWN,and search the...
5
by: muskie | last post by:
I've looked through as many posts about this as possible, but all end with no resolution. I simply need records from a table in random order, and I will be calling this recordset in a SQL...
1
by: Steven D'Aprano | last post by:
On Wed, 22 Oct 2008 16:59:45 -0400, Terry Reedy wrote: With respect Terry, I think what you have missed is the reason why the OP thinks this is a bug. He's not surprised that buffering is going...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.