473,474 Members | 1,348 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Finding a StreamReader's true position

I'm using a StreamReader to read in several lines from an ASCII file. I'd
like to know the StreamReader's "true" position-- that is, the number of
bytes into the file that the StreamReader has read. I thought about using
MyStreamReader.BaseStream.Position, but this always seems to return a
multiple of the StreamReader's buffer size (which seems natural-- as I
understand it the StreamReader reads from the underlying stream in discrete
blocks corresponding to the buffer size). I also considered counting the
bytes myself (as I read into the file), but ReadLine() doesn't record whether
the returned line ended in a CR, an LR, or both.

Any ideas?

Thanks,
Keith Kingsley
--
What if there were no hypothetical situations?
Nov 16 '05 #1
2 12977
Hi,

You can't using Streamreader for the same reason you expose. the solution
may be using a stream that does gives you the Position and then decorate it
with streamreader to have the "line" functionality.

Use a FileStream to open the file and them create a streamreader around it.
you could use FileStream.Position to know the position.

Even so, this may not be the solution, as the FileStream.Position will give
you what streamreader has readed, not what you consume.
Try it, if it does not work. you have to implement it . I think I have such
a code, I used it for a PPC application to read from a networkstream and
have a ReadString () like method. let me know if you need the code
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Keith Kingsley" <Ke***********@discussions.microsoft.com> wrote in message
news:7F**********************************@microsof t.com...
I'm using a StreamReader to read in several lines from an ASCII file. I'd
like to know the StreamReader's "true" position-- that is, the number of
bytes into the file that the StreamReader has read. I thought about using
MyStreamReader.BaseStream.Position, but this always seems to return a
multiple of the StreamReader's buffer size (which seems natural-- as I
understand it the StreamReader reads from the underlying stream in
discrete
blocks corresponding to the buffer size). I also considered counting the
bytes myself (as I read into the file), but ReadLine() doesn't record
whether
the returned line ended in a CR, an LR, or both.

Any ideas?

Thanks,
Keith Kingsley
--
What if there were no hypothetical situations?

Nov 16 '05 #2
Here is the code
public string ReadStringFromNetwork( )
{
StringBuilder buff;
try
{
buff = new StringBuilder( 20);
int ch;
while( (ch=networkstream.ReadByte())!= -1)
{
if (ch == 13) continue;
if ( ch==10) return buff.ToString();
buff.Append( Convert.ToChar( ch));

}
}
catch(Exception e)
{
throw new Exception("\n==>Method: NetAccess.ReadStringFromNetwork,
reading string :" + e.Message );

}
return buff.ToString();
}
PLEASE NOTE:
the code above assume that each character is 1 byte long, this is not true
with unicode so you have to be careful with this.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Nov 16 '05 #3

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

Similar topics

20
by: Webdad | last post by:
Hi! I running my first year as industrial engineer (informatics) We have an assignment to do : .... create a playfield (matrix). Some places in that field are blocked, so you can't pass them....
1
by: andrewcw | last post by:
OK I am half way there - I can manipulate the stream without the byte issue like this - but is this the way to push the new values back into the stream & write out the stream without resorting to...
3
by: Jaga | last post by:
Hi, how can I read the same passage in a textfile several times? I'm writing a little textgenerator. It reads lines from a file, replaces the variables, and writes it in an other file. Some...
5
by: Anders Olsson | last post by:
I need to create a StreamReader which starts from the position of an existing StreamReader. This way, the second StreamReader can read lines "ahead" in an own loop, and then I can go back to the...
2
by: Tarren | last post by:
Hi: I want to check my text file to ensure the last line has only DONE on it, prior to loading the file. How can I do this with a StreamReader object to go to the last line so I can perform...
21
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the...
7
by: Eric | last post by:
I am trying to save the "last read" position of a file using a StreamReader object. I am reading the lines of the file using StreamReader.ReadLine and then saving the current position in the...
1
by: Peter | last post by:
'if you do this Dim sr As New StreamReader _(System.AppDomain.CurrentDomain.BaseDirectory& "CR.txt") ' and then loop through values while sr.peak > -1 ...... end while
9
by: lord.zoltar | last post by:
Hi, I'm having some trouble with a StreamReader. I use a System.IO.StreamReader to read a text file and then print it. If the user has selected a range of pages starting past the first page, I...
0
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,...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.