473,379 Members | 1,187 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,379 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 3109
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...

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.