1

I am appending a very large text file, however when it is complete and I go to view it i get the following error. Null characters (00H) contained in file. This is the code I'm using to read all the text files to one large text file. I am opening the large file in EmEditor.

Any help gratefully received.

    Dim Paths() As String = Directory.GetFiles("c:\logs1", "main_access_log.*", SearchOption.AllDirectories)
    System.Windows.Forms.Cursor.Current = Cursors.WaitCursor

    For Each Path As String In Paths
        File.AppendAllText("c:\logs1\zmaster.txt", File.ReadAllText(Path), Encoding.Default)
    Next
    System.Windows.Forms.Cursor.Current = Cursors.Default
    MessageBox.Show("Master File Created")
vbvirg20
  • 115
  • 12
  • The code looks correct. Are you sure there are no null characters in any of the original files? Otherwise there may be an encoding problem but it doesn’t look like it. – Konrad Rudolph Jul 07 '15 at 12:47
  • Hi Konrad, how can i check the original file for the problem and if i find it do i do a simpe fine and replace – vbvirg20 Jul 07 '15 at 12:49
  • Well since EmEditor is reporting the problem (isn’t it?), have you tried opening the original files in EmEditor? As for a fix, that depends on why the null character is included in the first place but yes, a find&replace may be a good solution. – Konrad Rudolph Jul 07 '15 at 12:50
  • You are correct the original file does ask me the file format and has the "Null characters (00H) contained in the file. So the issue is there in the text file but how do i find the actual character to do something about it? – vbvirg20 Jul 07 '15 at 12:55
  • Text files on Windows are oftentimes encoded as UTF-16, which is a multi-byte encoding. Standard ASCII characters are stored in UTF-16 with an extra NUL byte per character. If you open a UTF-16 text file with a single-byte encoding, such as UTF-8 or Windows-1252, it will show a NUL byte between every character. Is that your problem? Does opening the file as UTF-16 fix it? (If you have a choice between UTF-16LE and UTF-16BE, try UTF16-LE first. Also, UTF-16LE is often just called "Unicode" in Windows, such as when opening a file in Notepad.) – Lithis Jul 07 '15 at 13:11
  • 1
    Also, the documentation for [Encoding.Default](https://msdn.microsoft.com/en-us/library/system.text.encoding.default%28v=vs.110%29.aspx) says "using the default encoding is generally not recommended. To ensure that encoded bytes are decoded properly, you should use a Unicode encoding, such as UTF8Encoding or UnicodeEncoding, with a preamble." Have you tried appending the text with `Encoding.UTF8` or `UTF8Encoding.UTF8Encoding` instead (or whatever encoding is appropriate for your file)? – Lithis Jul 07 '15 at 13:18
  • Finally, [`File.ReadAllText`](https://msdn.microsoft.com/en-us/library/System.IO.File.ReadAllText%28v=vs.110%29.aspx) has an overload that accepts an `Encoding` parameter. The overload without the `Encoding` parameter tries to guess the encoding of the file it is reading, but it isn't always correct. Try passing the correct encoding to `ReadAllText`, for example `Encoding.Unicode`. – Lithis Jul 07 '15 at 13:29
  • Even as Encoding.UTF8 it still comes up with the error, – vbvirg20 Jul 07 '15 at 14:09
  • You mentioned that opening the original file in EmEdit gave you the Null character warning. What encoding did you choose when you opened the file? Have you tried opening it with different encodings? And does it show you where the NUL bytes are, or are they invisible? If you have a problem with your input, or if your input isn't in the encoding you expect, you won't get the right output, so you should figure out what encodings the files c:\logs1\main_access_log.* are. – Lithis Jul 07 '15 at 14:30
  • The Null comment is invisible i have tried many different ways of opening it, it will open if i choose to use a different encoding, but then if i run code across the text file it only gets as far as the (00H) then bombs out. – vbvirg20 Jul 07 '15 at 14:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82621/discussion-between-lithis-and-vbvirg20). – Lithis Jul 07 '15 at 14:41

0 Answers0