7

StreamWriter Writing NUL characters at the end of the file when system shutdown...

 2 years ago
source link: https://stackoverflow.com/questions/10913632/streamwriter-writing-nul-characters-at-the-end-of-the-file-when-system-shutdown
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
StreamWriter Writing NUL characters at the end of the file when system shutdown abruptly

I am writing some test application to write some text into a text file using StreamWriter. While executing the WriteLine method system shutdown abruptly. After rebooting the machine have observed that there are many NUL characters at the end of file.

Have searched many sites including MSDN I didn't found the solution for this problem.

Could any one help me to resolve this problem.

This can be easily reproducible if we do the following steps:

  1. Create WindowsApplication and place a button control on it.
  2. In the button click event handler write the following code:

    private void button1_Click(object sender, EventArgs e)

    {
        string str = "Welcome to the C Sharp programming world with a test application using IO operations.";
        StreamWriter sw = new StreamWriter(fileName, true, Encoding.Unicode, str.Length);
        sw.WriteLine(str);
        sw.Close();        
    } 
    
  3. Run the application and click on the button continuously (Do not until machine shutdown) and press the Poweroff button of the PC.

  4. Reboot the PC and check the file it contains the following text:

Welcome to the C Sharp programming world with a test application using IO operations.

Welcome to the C Sharp programming world with a test application using IO operations.

Welcome to the C Sharp programming world with a test application using IO operations.

Welcome to the C Sharp programming world with a test application using IO operations.

Welcome to the C Sharp programming world with a test application using IO operations.

Welcome to the C Sharp programming world with a test application using IO operations.

NULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNUL

The NUL characters will appear in NotePad++ and we cannot see these characters in Normal NotePad.

Thanks in Advance


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK