zoukankan      html  css  js  c++  java
  • fread fwrite文本模式读写回车换行符 自动转换问题

    fread  会把 (0d0a)替换为
    fwrite 会把 替换为 (0d0a), 会变成 (0d0d0a)

     

    今天在写一个日志类,用于打印服务程序的信息。

    我将每一个日志信息都以单行的形式输入,所以在开头加上了回车换行符。
    文件是以代码如下:
    FILE *file = fopen(log_file_name,"a+");
    if (!file)return;
    fwrite("
    ",3,file);//这里不是原始代码,只用来说明问题

    然后用winhex软件查看了十六进制的数据,结果发现 对应的十六进制为0D 0D 0A。
    这很明显,和我的预期不一样。
    然后查看了msdn,msdn对于fwrite的描述是,以文本模式打开文件的时候,写入时会自动将 转换为 ,不对 进行处理。
    msdn的错误描述,可能会误导很多人吧。msdn说的是对 进行转换,其实是对 进行转换,经过实际测试得到的。
     
    下面是2008-MSDN:

    Remarks

    The fwrite function writes up to count items, of size length each, from buffer to the output stream.

    The file pointer associated with stream (if there is one) is incremented by the number of bytes actually written.

    If stream is opened in text mode, each carriage return is replaced with a carriage-return – linefeed pair.

    The replacement has no effect on the return value. 

    下面是在线-MSDN: 修正了上面的错误

    Remarks

    The fwrite function writes up to count items, of size length each, from buffer to the output stream. The file pointer associated with stream (if there is one) is incremented by the number of bytes actually written. If stream is opened in text mode, each line feed is replaced with a carriage return-line feed pair. The replacement has no effect on the return value.

    When stream is opened in Unicode translation mode—for example, if stream is opened by calling fopen and using a mode parameter that includes ccs=UNICODE, ccs=UTF-16LE, or ccs=UTF-8, or if the mode is changed to a Unicode translation mode by using _setmode and a mode parameter that includes _O_WTEXT, _O_U16TEXT, or _O_U8TEXT—buffer is interpreted as a pointer to an array of wchar_t that contains UTF-16 data. An attempt to write an odd number of bytes in this mode causes a parameter validation error.

    Because this function locks the calling thread, it is thread-safe. For a non-locking version, see _fwrite_nolock.

     

    备注

     

    Fwrite函数将每个项的大小缓冲区写入到输出, 并对其进行计算。 关联的文件指针 (如果有) 以实际写入的字节数为增量递增。 

    如果在文本模式下打开, 则会将每个换行符替换为回车换行符对。 该替换不会影响返回值。

     

  • 相关阅读:
    新浪微博OAuth2.0 VS OAuth1.0 主要区别总结
    NSCondition的用法
    iOS $99 刀 开发者证书的申请步骤
    three20 报出 文件 no such file or directory的原因以及解决方案
    three20.bundle以及 sharekit.bundle多语言无法生效的解决方案
    NSThread的使用
    iOS中switchcase的优化用法
    iOS开发中生成随机数方法的选择
    面向对象软件设计原则(五) —— 应用示例
    普通软件项目开发过程规范(四)—— 控制和结束阶段
  • 原文地址:https://www.cnblogs.com/hjbf/p/11466485.html
Copyright © 2011-2022 走看看