Resolved Garbage written in StreamWriter

Gloops

Well-known member
Joined
Jun 30, 2022
Messages
137
Programming Experience
10+
Hello everybody,

With this instruction :
StreamWriter writing instruction:
sw.WriteLine($"{DateTime.Now} ; {iNbTickets,2}");

a log is written.

This application works quite well for several months, it is on its third machine now. A few lines written per week.

On one line, a certain number of null characters have been added at the beginning. So, the first field (to the ';') could not be parsed to a date.

I did not imagine that at the beginning, so I did not manage the exceptions.

In case it happens again, in the try...catch I replace \0 by \x3, to display the problematic line, with little squares in place of the null characters.

And then I suppress them in a notepad.

Any idea what happened and whether I should do anything else?

Oh, one precision: this did not happen on the last line, the file has been read successfully several times since the line was created.

So, maybe the null characters have been added later.

Hum, the machine is two weeks old, I hope it is not already weak ...

In case it helps, Microsoft's PowerToys are installed. Their spelling corrector is sometimes surprising.

That being said, the file is rarely accessed outside the C# application.
 
Last edited:
What is the type of iNbTickets?

(It's hard to guess since you use Hungarian notation despite the .NET Framework recommendations not to use Hungarian, as well as you invented your own Hungarian notation different from the Windows System Hungarian.)

Anyway, assuming the following:
  1. The code you presented above is the only code writing to the file;
  2. iNbTickets is not some string type;
  3. You are not using a custom culture where the date time formatting includes characters outside of the ASCII range; and
  4. You didn't override the default StreamWriter encoding of UTF-8.
Then that stream corruption is not due to your writing to your log file using the StreamWriter.

Are you sure that you didn't accidentally open the file in Notepad and resaved it as Unicode? That's the only way I can think of getting any '\0's into the file.

As for '\3's, that is the ASCII code for ETX (End of Text). Windows normally doesn't use that, but is it possible if you have some *nix like utilities that have also been writing to your log file?
 
What is the type of iNbTickets?

Oh sorry, it is an integer.
Traditionally I should rather have written intNbTickets, but recently in the examples in the documentation, I often saw variables names with just one character for the type, so I followed the (bad) example.

Anyway, assuming the following:
  1. The code you presented above is the only code writing to the file;
  2. iNbTickets is not some string type;
  3. You are not using a custom culture where the date time formatting includes characters outside of the ASCII range; and
  4. You didn't override the default StreamWriter encoding of UTF-8.

That is right.

Opening the StreamWriter:
    using (StreamWriter sw = new StreamWriter(
                        Path.Combine(Application.LocalUserAppDataPath, "JournalBus.txt"),
                        true, Encoding.Default, 80)
                    )

Then that stream corruption is not due to your writing to your log file using the StreamWriter.
(Notepad ...)

I also thought of something like that, also I had no idea that Notepad was able to insert null characters. Notepad2 displays them properly. On the current machine I did not associate txt files to Notepad2 yet, so it can be any of both depending of how I opened it. I have absolutely no remembering of it, that being said. But it is true that I had no healthy life those last weeks.

As for '\3's, that is the ASCII code for ETX (End of Text). Windows normally doesn't use that, but is it possible if you have some *nix like utilities that have also been writing to your log file?

The garbage characters that were added in the file were null characters, but those are not accepted by MessageBox, so I replaced them by \x3 to be able to display the line. The test was OK.
 
If you are using .NET Framework (rather than .NET Core), Encoding.Default is not UTF-8.
 
Oh, I was confused on this, I answered correct to point 4 "you did not change the default Streamwriter encoding".
But I did not doubt about which one you suggested for it.
 
If you look at the documentation for StreamWriter, if you don't specify the encoding, the default encoding will be UTF-8 regardless of framework version or platform.

By passing Encoding.Default, you are overriding the default encoding used by StreamWriter and now passing in an encoding that is dependent on what version of the framework you are using.
 
So, to sum up, there is no surprise, we see the source of the null characters the same way.
I have got to see whether something causes an instability on my machine.
PowerToys took several hours to install, and I am not sure about their spell checker. As Firefox often slows down, and sometimes even crashes, I have got to see whether all that is linked.
 

Latest posts

Back
Top Bottom