fopen时w与wb的区别

    今天做了个小程序,把一个二进制文件分割为多个1.44M的文件,但分割完成后,发现很多文件的大小都超过1.44M。在网上搜索了下,发现时fopen是的参数部队。
    “w”表示为文本文件。用则会把文件中的“0A”变为“0D0A”,因为某些文件系统认为“0A”为文本文件的换行符,windows认为“0D0A”为文本文件的换行符,为了兼容其他文件系统(如从linux拷贝来的文件),windows上的fopen函数做了这样的格式转换。如果我记得没错的话,linux与vxworks上不会做这样的转换,所以可以说使用“w”,其属性要看所在的平台。
    “wb参数”表示为二进制文件,则样文件系统会按纯粹的二进制格式进行操作,因此也就不存在格式转换的问题了。
     综上,如果使用的是二进制文件,一定要使用“wb”参数,这样肯定错不了。
    
参数“wt”:
    “w”其实默认就是“wt”,即write in text mode。下面是msdn中队这几个参数的解释:

“If t or b is not given in mode, the default translation mode is defined by the global variable _fmode. ”

“The default setting of _fmode is _O_TEXT for text-mode translation. _O_BINARY is the setting for binary mode.

You can change the value of _fmodein either of two ways:

  • Link with BINMODE.OBJ. This changes the initial setting of _fmode to _O_BINARY, causing all files except stdin, stdout, and stderrto be opened in binary mode.
  • Change the value of _fmode directly by setting it in your program. ”
原文地址:https://www.cnblogs.com/zhoug2020/p/2428918.html