473,808 Members | 2,838 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Nov 15 '05 #1
8 6829
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.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

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_@_mod o_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*******@disc ussions.microso ft.com> wrote in message
news:06******** *************** ***********@mic rosoft.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.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

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******** ********@TK2MSF TNGP12.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_@_mod o_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*******@disc ussions.microso ft.com> wrote in messag
news:06******** *************** ***********@mic rosoft.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.P osition
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 RandomAccessFil e 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******** *****@TK2MSFTNG P11.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
2407
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 sys
1
4413
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 warning.) building 'readline' extension gcc -pthread -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -fPIC -fno-strict-aliasing -I. -I/nfs/gs/home/jianq/Python-2.4.2/./Include -I/gs/home/jianq/include -I/usr/local/include
6
2791
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 amount of data must accumulate on sys.stdin before even my first invocation of readline() returns. This delay prevents my program from being responsive in the way it must be.
2
6422
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. http://www.sgi.com/tech/stl/random_access_iterator_tag.html Any pointer to an example is appreciated.
3
3028
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 of the Web Services data) in XML format. Since the size of the XML local store could be rather large, I'd prefer to have a random access mechanism for reading and writing to it. It seems that XMLReader /XMLWriter are sequentially fast,...
6
5479
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 my website which allows users to define parameters and return results accordingly. The problem i have is a need to return these results in a random order each time. With SQLServer i know NEWID() would do the trick - used this many times before...
25
2794
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 history. I want the upand down key to act the same as it would in a unix terminal. If anyone could help me, I would greatly appreciate it. Thank you in advance, Natalie
5
8161
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 statement from ASP. I've tried the following but it does not produce random order: SELECT * FROM Table1 ORDER BY Rnd(TableID) ASC; where TableID is an autonumber field. I've seen references to Randomize, but how do you use Access's
1
236
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 on: "This indicates some sort of buffering and caching is going on." but he thinks that the buffering should be discarded when you seek:
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10628
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10373
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9195
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5547
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.