I'm creating files on an IIS 5.0 web server, and when I create files with
the FileSystemObject, it creates files that have ASCII 0's as every other
character.
For example, if I TextStream.Write("Hello"), the resulting file has:

(gibberish) H (null) e (null) l (null) l (null) o (null)

In a text editor, the output looks fine, like "Hello".
In a hex editor, the file looks like this:

FF FE 48 00 65 00 6C 00 6C 00 6F 00

Anyone have an idea what's going on? It's driving me nuts... I'm not sure
where the "FF FE" comes from either.

Thanks,
Dan
www.dantan.net

Re: Unintentional ascii 0 (nulls) when creating file with FSO? by Christoph

Christoph
Wed Sep 08 18:45:06 CDT 2004

08.09.2004 23:04, Dan Tan schrieb:
> I'm creating files on an IIS 5.0 web server, and when I create files with
> the FileSystemObject, it creates files that have ASCII 0's as every other
> character.
> For example, if I TextStream.Write("Hello"), the resulting file has:
>
> (gibberish) H (null) e (null) l (null) l (null) o (null)
>
> In a text editor, the output looks fine, like "Hello".
> In a hex editor, the file looks like this:
>
> FF FE 48 00 65 00 6C 00 6C 00 6F 00
>
> Anyone have an idea what's going on? It's driving me nuts...

The TextStream is opened as Unicode.
Some people mix up CreateTextFile and OpenTextFile params.
As for CreateTextFile, the third-Param (Tristate) stands for Unicode
vs Ascii, while for the OpenTextFile-Method the third param is
boolean and flags OverwriteExisting.
So:

Set stream = fs.CreateTextFile("C:\my.txt", true, true)

will open the file as Unicode, while

Set stream = fs.CreateTextFile("C:\my.txt", true, false) 'OR:
Set stream = fs.CreateTextFile("C:\my.txt", true) 'OR:
Set stream = fs.OpenTextFile("C:\my.txt", 2, true)

will open it as ASCII. ASCII is default.


> I'm not sure where the "FF FE" comes from either.

So called "Byte-Order-Mark" (->BOM) for Unicode-Files. It declares
the ByteOrder. FF EE is Little Endian for UTF-16.

http://www.opentag.com/xmli18nfaq.htm#enc_bom

--
Gruesse, Christoph

Rio Riay Riayo - Gordon Sumner, 1979

Re: Unintentional ascii 0 (nulls) when creating file with FSO? by Keven

Keven
Thu Sep 23 16:22:18 CDT 2004

I am having the same issue, as well as the FF FE starting off the file. I would love to know if there is a way of stopping that from happening using the code above. As for now, I have had luck using a different method for writing the file, as shown here:

set fs=Server.CreateObject("Scripting.FileSystemObject")
set tfile=fs.CreateTextFile(fileLocation)
tfile.WriteLine(data)
tfile.close
set tfile=nothing
set fs=nothing

Of course, looping through the data in the writeline over and over.. this is messier coding, I find than collecting the data into a variable and then ejecting it to a text file later, so if there is a solution or at least a reason as to why the output gets messy the other way, please let me know, if you know!

**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...

Re: Unintentional ascii 0 (nulls) when creating file with FSO? by Keven

Keven
Thu Sep 23 17:53:55 CDT 2004

Found the fix.. ends up that we were outputting the text file in unicode.

So look at the docs:
http://msdn.microsoft.com/library/en-us/script56/html/jsmthcreatetextfile.asp


Make sure that the constants for writing are set properly.

**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...