Ques on FileStream

  • Thread starter Thread starter Harsh Thakur
  • Start date Start date
H

Harsh Thakur

Hi,

FileStream.ReadByte() returns an int.
How do I convert this to its character/string equivalent.

TIA
Harsh Thakur
 
Harsh Thakur said:
FileStream.ReadByte() returns an int.
How do I convert this to its character/string equivalent.

You can use the 'Chr' function to do that. Notice that a single byte
can also be part of a character. You may want to use the 'StreamReader'
class with an appropriate encoding instead.
 
Herfried K. Wagner said:
You can use the 'Chr' function to do that.

You *can*, but I'd advise against it, mainly because it makes it a lot
less clear what character encoding you're expecting.
Notice that a single byte
can also be part of a character. You may want to use the 'StreamReader'
class with an appropriate encoding instead.

Yup - much better :)
 
Thanks a lot guys.
Jon, I used the TextReader and it worked.

But, lets assume I don't care about the encoding.
Then I can just typecast the return value of Read() into a
char. Is there a problem with this approach?

I have another question.
How can I know/change the exact position of the underlying
stream. I tried using StreamReader.BaseStream to seek and
to get the current position. But it is not reliable at all.
It seems to jump in multiples of 1024.

Is there a reliable way to seek to an exact location.

Regards
Harsh Thakur
 
Harsh Thakur said:
Thanks a lot guys.
Jon, I used the TextReader and it worked.

But, lets assume I don't care about the encoding.

Then don't bother reading from the file at all, and make every
character 'x'. Seriously, that's about as useful - not caring about the
encoding, but still wanting the text data, is like opening "a music
file" and wanting the music out of it, but assuming you don't need to
know whether it's stored in MP3, WMA, WAV etc format. (Or rather, it's
worse - at least those formats tell you *in the file* what format they
are.)
Then I can just typecast the return value of Read() into a
char. Is there a problem with this approach?

Yes. It means you're assuming ISO-8859-1, in fact. Is that definitely
the encoding you want? If so, you might as well just use the
appropriate encoding.
I have another question.
How can I know/change the exact position of the underlying
stream. I tried using StreamReader.BaseStream to seek and
to get the current position. But it is not reliable at all.
It seems to jump in multiples of 1024.

That would be because it will have buffered data.
Is there a reliable way to seek to an exact location.

What location do you want to get to - character, or byte? There may
well be a difference, and it wouldn't be a good idea to seek to half
way through a multi-byte UTF-8 character, for instance. How large is
the file? Could you just read the whole thing and then "seek" to
different parts of the string instead?
 
Hi Jon,

Thanks for the reply.
I take your point about the encoding. You made it very
nicely :-).

About seeking, I don't think its a very efficient
solution. Its definitely an alternative though.

Regards
Harsh Thakur
 
Harsh Thakur said:
About seeking, I don't think its a very efficient
solution. Its definitely an alternative though.

One thing I forgot to mention before (sorry!): you can use
StreamReader.DiscardBufferedData when you seek. From the docs:

<quote>
Use DiscardBufferedData to seek to a known location in the underlying
stream and then begin reading from this new point, or to read the
contents of a StreamReader more than once. Because this method can
cause slower performance, use it sparingly and only when you need it
for a specific scenario.
</quote>
 
Back
Top