当系统突然关闭时,StreamWriter在文件末尾写入NUL字符 [英] StreamWriter Writing NUL characters at the end of the file when system shutdown abruptly

查看:92
本文介绍了当系统突然关闭时,StreamWriter在文件末尾写入NUL字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些测试应用程序,以便使用StreamWriter将一些文本写入文本文件.在执行WriteLine方法时,系统突然关闭.重新启动计算机后,发现文件末尾有许多NUL字符.

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.

已经搜索了许多站点,包括MSDN,但我没有找到解决此问题的方法.

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. 创建WindowsApplication并在其上放置一个按钮控件.
  2. 在按钮click事件处理程序中,编写以下代码:

  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(对象发送者,EventArgs e)

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();        
} 

  • 运行该应用程序,然后连续单击该按钮(直到计算机关闭后才单击该按钮),然后按一下PC的Poweroff(电源关闭)按钮.

  • Run the application and click on the button continuously (Do not until machine shutdown) and press the Poweroff button of the PC.

    重新启动PC并检查其中包含以下文本的文件:

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

    欢迎使用IO操作的测试应用程序进入C Sharp编程世界.

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

    欢迎使用IO操作的测试应用程序进入C Sharp编程世界.

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

    欢迎使用IO操作的测试应用程序进入C Sharp编程世界.

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

    欢迎使用IO操作的测试应用程序进入C Sharp编程世界.

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

    欢迎使用IO操作的测试应用程序进入C Sharp编程世界.

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

    欢迎使用IO操作的测试应用程序进入C Sharp编程世界.

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

    NULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNUL

    NULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNUL

    NUL字符将出现在NotePad ++中,而我们在普通NotePad中看不到这些字符.

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

    预先感谢

    推荐答案

    我在Linux(在树莓派上)上的 netcore 遇到了同样的问题.看起来它们是某种未及时刷新的缓冲区.( StreamWriter.Flush()方法无济于事).

    I had the same problem with netcore on linux (on a raspberry). It look like their is some sort of buffer that are not flushed in time. (StreamWriter.Flush() method doesn't help).

    解决方法是将缓冲区大小设置为数据大小 array.Length ,并使用 FileOptions.WriteThrough 禁用所有缓存(来自msdn:)

    The workaround is to set the buffer size to the data size array.Length and disable all caching with FileOptions.WriteThrough (from msdn:)

    指示系统应写入任何中间缓存并直接进入磁盘.

    Indicates that the system should write through any intermediate cache and go directly to disk.

    string str = "Welcome to the C Sharp programming world with a test application using IO operations.";
    byte[] data = new UTF8Encoding(true).GetBytes(str);
    using (FileStream fs = new FileStream(fileName, FileMode.Append, FileAccess.Write,
                                    FileShare.Read, data.Length, FileOptions.WriteThrough))
    {                    
        fs.Write(data , 0, data.Length);
    }
    

    对于那些来自C/C ++的用户,请设置posix标志:

    For those coming from C/C++ it set the posix flag:

    O_DIRECT尝试最大程度地减少往返于此的I/O的缓存影响文件.通常,这会降低性能,但在以下方面很有用特殊情况,例如应用程序自行缓存时.文件I/O是直接在用户空间缓冲区中进行的.

    O_DIRECT Try to minimize cache effects of the I/O to and from this file. In general this will degrade performance, but it is useful in special situations, such as when applications do their own caching. File I/O is done directly to/from user space buffers.

    更多信息: 查看全文

  • 登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆