The terminating char is the null character '\0'.
It is database file. Eveything works except that I can't get the strings
read properly.
This seems to work:
private string ReadUtf16String()
{
try
{
string readString = String.Empty;
char c = binReader.ReadChar();
while(c != '\0')
{
readString += c.ToString();
c = binReader.ReadChar();
}
return readString;
}
catch(EndOfStreamException)
{
eof = true;
return null;
}
catch(Exception ex)
{
throw new Exception("Error reading a UTF-16 string.", ex);
}
}
However, for some reason some strings have two bytes in front of them
that tell the size I suppose? Is that a standart for UTF-16 strings or
is this something that is limited to this particular file. Is there a
way to distinguish between the strings that have these two bytes and the
ones that dont? (this is the main part that is causing the trouble)
Should I dispose of those two bytes or are they part of the string?
I undestand that these questions might be relavant only in this
database, but you have any advice, I will greatly appreciate it.
Thanks,
Nick Z.
Nick Z. said:
I am having trouble with the StreamReader.
As far as I can see there is no way to read a string.
Really I dont know why this is so hard.
In the file there is a simple string terminated with three empty bytes.
Is there no function like ReadString() anywhere? That would simply read
a unicode string untill it gets to a terminating character.
What exactly do you mean by "terminating character"?
StreamReader:
ReadLine method reads the null bytes and goes on to read another 1000
bytes untill it gets to a 0x0D or something in that order.
Indeed.
Read() is described to retun the next character yet it returns an int?
What? Casting the int into a char doesnt seem to work...
Read(char[],int,int) reads the string fine (i think), asuming I found
the length of the string before hand. However, right after the mehtod
returns the Position property of the BaseStream is now fast-forwarded a
1000 bytes or so when only 35 characters were read.
Yes.
ReadBlock() is the same as Read(char[],int,int) as far as I can see.
BinaryReader:
ReadString(), oh yeah I thought. However, this is straight from the docs
"The string is prefixed with the length, encoded as an integer seven
bits at a time.". Isnt this UTF-7? In any case it doesnt work for me.
No, it's not UTF-7. Only the *length* is encoded as an integer seven
bits at a time.
So what are my options at this point?
Well, you could start by telling us what your file format is. It sounds
like it's a mixture of binary and text, which is bad news to start with
I'm afraid.