473,385 Members | 1,676 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,385 software developers and data experts.

StreamReader - Multibybe chars - Seek()

I'm writing a home grown csv text file search, have sorted "id" in the first
"column". Other info after that in the "row". I seek half way thru the
file, get to a row boundry, determine "id" that is there, determine if that
id is higher or lower, cut seek range in half, etc, until I find the row I
am looking for.

This works very well, I encoded the file with unicode so all chars will
always be 2 bytes, since seek() goes on byte basis and read goes on char
basis. Is there any "IsLeadByte()" sort of method in C# so that I can keep
files UTF8? (I think that is the default) Unicode works well, but nearly
doubles the file size, and it is only about 90% more than the default
encoding, so I'm guessing there are some 2 byte chars in the default.

Thanks in advance!

Derrick
Nov 16 '05 #1
1 3112
Derrick <de*********@excite.com> wrote:
I'm writing a home grown csv text file search, have sorted "id" in the first
"column". Other info after that in the "row". I seek half way thru the
file, get to a row boundry, determine "id" that is there, determine if that
id is higher or lower, cut seek range in half, etc, until I find the row I
am looking for.

This works very well, I encoded the file with unicode so all chars will
always be 2 bytes, since seek() goes on byte basis and read goes on char
basis. Is there any "IsLeadByte()" sort of method in C# so that I can keep
files UTF8? (I think that is the default) Unicode works well, but nearly
doubles the file size, and it is only about 90% more than the default
encoding, so I'm guessing there are some 2 byte chars in the default.


It's quite easy to tell the first byte in a UTF-8 character: either its
top bit is unset, or its next-to-top bit is set.

The diagram in http://www.cl.cam.ac.uk/~mgk25/unicode.html
would probably help you to see what I mean.

Unfortunately, that doesn't help you a lot for seeking - basically, you
*can't* seek to a particular character in a UTF-8 stream without
examining virtually all the bytes in between.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

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

Similar topics

0
by: bullshark | last post by:
The code ran for 133 hours without failing. This would be the 267th iteration of the (same) object and the code below is used 5 times per iteration giving 1335 cycles. It was halfway through...
1
by: Derrick | last post by:
I'm writing a home grown csv text file search, have sorted "id" in the first "column". Other info after that in the "row". I seek half way thru the file, get to a row boundry, determine "id" that...
3
by: VMI | last post by:
When I execute a ReadLine from an ascii file with special chars (ie. the 'Ñ' in "NUÑEZ PEREZ"), it automatically deletes this character. So "NUÑEZ PEREZ" becomes "NUEZ PEREZ". How can this be...
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...
11
by: Tiger | last post by:
We can use seek() in the FileStream class,as we know. But I found that seek() is not work correctly in StreamReader. Who can tell me how to use seek() correctly in StreamReader? thanks a lot! I...
1
by: Dan | last post by:
Hi, I'm having a problem with StreamReader.Peek(). Let's say I open a file and read it to end; then I'd want to move its stream pointer back to the file beginning: I can call BaseStream Seek method...
7
by: Drew Berkemeyer | last post by:
Hello, I'm using the following code to read a text file in VB.NET. Dim sr As StreamReader = File.OpenText(strFilePath) Dim input As String = sr.ReadLine() While Not input Is Nothing...
2
by: Joan Reddy | last post by:
Can anyone tell me why this code doesn't work for setting the pointer to the begining of a file stream? This is driving me crazy. At the end of Main1, sString2 is the second line of the file, as...
2
by: dave | last post by:
Hi. I am tring to read from random parts of an SDF file using a stream reader. I know each line in my file is 30 chars long so I know where in the file I want to go to read a specific record. The...
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.