Is there any difference between text and binary mode in file access?

拈花ヽ惹草 提交于 2020-01-02 03:46:28

问题


Is there any difference if I open a file in text mode rather than binary mode? Because I read that UNIX and Linux make no distinction between text and binary files.


回答1:


There is no difference on Linux (at least on native file systems like Ext4 and on most other file systems too, with the usual GNU libc).

Perhaps some bizarre filesystems could have a specific flag to open differently binary or text files. I know no such filesystem. Maybe you could code some FUSE filesystem making the distinction, perhaps with some additional hack around fopen inside a bizarrely customized libc

However, C99 standard (at least page 271, §7.19.5.3 of n1256 draft) mentions explicitly the text vs binary mode, so your program would be easier to port to other systems (such as Windows) if it conforms to the standard.

So my point is that you might want to try passing a mode string to fopen which differentiate the binary vs text mode. (I agree that I don't do that very often). It won't hurt.

The Linux fopen(3) man page explicitly says:

The mode string can also include the letter 'b' either as a last character or as a character between the characters in any of the two- character strings described above. This is strictly for compatibility with C89 and has no effect; the 'b' is ignored on all POSIX conforming systems, including Linux. (Other systems may treat text files and binary files differently, and adding the 'b' may be a good idea if you do I/O to a binary file and expect that your program may be ported to non-UNIX environments.)

Of course, the open(2) syscall does not have any way of transmitting a mode flag. (You 'll need some private ioctl(2) probably)



来源:https://stackoverflow.com/questions/25823310/is-there-any-difference-between-text-and-binary-mode-in-file-access

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!