Re: StreamWriter and BinaryWriter

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"philip" <phil@xxxxxxxxxxxx> schrieb
If I execute that :

Dim Temp as string = "This is a text"
Dim sw As StreamWriter
Dim fullFileName as string = "c:\text.txt"
sw = New StreamWriter(fullFilename)
sw.Write(temp)
sw.Close()

the resulting file 'Text.txt' has the same length than the string
'temp'. Idem if I Use BinayWriter.

BUT if 'temp' contains others characters than readable character,
then the result file has not the same length than the string.
So, if temp contains "ÿØÿá" (that is to say a string concanated
with the 4 characters chr(255) & chr(216) & chr(255) and chr(225)),
the file writen is 8 octets long, and not 4 octets like the string
(twice more in the file than in the string). Consequently, the copy
of the memo field in a file to rebuild the 'img' file is wrong and
image reconstructed is not visible. In Access with the method "open
for binary", all is good.

So I think that I don't use the good method in Visual Studio 2005 to
copy this string on a file without any change.

Can someone help me and tell me the methos to do that ?


Strings are stored as Unicode characters. Each character occupies 2 bytes. If you open the Streamwriter without specifying an encoding, it uses UTF-8 encoding. This means that some characters are stored as 1 byte per character and some as 2 bytes per character. In your example, all characters are stored as 2 bytes per character.

Specify a 1-byte-encoding if you want to do so. Usually System.Text.Encoding.Default is used:

sw = New StreamWriter(fullFilename, true, System.Text.Encoding.Default)

Be aware that conversion from a 2-byte character set to a 1-byte character set can lead to information loss. Thus, you should use exactly the same encoding to write the file as it has been used to read it.


Armin

.



Relevant Pages

  • [TOMOYO #15 3/8] Common functions for TOMOYO Linux.
    ... This file contains common functions (e.g. policy I/O, pattern matching). ... Since TOMOYO Linux is a name based access control, ... TOMOYO Linux's string manipulation functions make reviewers feel crazy, ... the Linux kernel accepts all characters but NUL character ...
    (Linux-Kernel)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)
  • Re: RfD: Escaped Strings
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... \b BS (backspace, ASCII 8) ... \ ** escapes to characters much as C does. ...
    (comp.lang.forth)
  • Re: A note on computing thugs and coding bums
    ... code is valid for any character set that is legal in C (which is a ... characters in the required source character set ... A String, in C Sharp or Java, can be redefined. ... allow programmers to handle some other data format, ...
    (comp.programming)