为什么我会打开一个文件(std :: ifstream)没有std :: ios :: binary? [英] Why would I ever open a file (std::ifstream) without std::ios::binary?

查看:519
本文介绍了为什么我会打开一个文件(std :: ifstream)没有std :: ios :: binary?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能属于Stack Exchange的不同部分,但我不这么认为 - programmers.se更多的是关于其他事情。



:有些事情你可以做std :: ios :: binary,你不能在文本模式(例如相对搜索),但我找不到任何事情在文本模式下,你不能做二进制模式 - 即使读取文件文本与std :: getline()



那么为什么我会以文本形式打开呢?作为一个或许相关的问题,为什么不默认打开为二进制?



这里是什么?导致我问:



我有一个文件在Windows系统上创建 - 也就是行尾是CR LF。



我使用 std :: ios :: binary std :: ifstream c> flag



我通过 std :: getline 解析文件, - getline一次读取一行。



系统:Windows 7 Pro



编译器:g ++ for MINGW32

解决方案

在文本模式下,你不能在二进制文件中做什么?阅读
文本,为初学者。以文本模式自动打开的文件
在内部翻译'\\\
'
字符,以及系统用于在外部对文件中的行分隔的
。它可以
也识别文件的任意结尾,即使
底层系统要求文件大小是某个
固定大小的倍数。



今天的选择有点复杂的事实,你
经常必须从不兼容的系统访问文件。如果
在Windows和Unix上都安装了一个文件系统,则在Windows下将
写为文本,在Unix下读为文本,然后在
中读出额外的字符。在这种情况下,可能是
比读取和写入二进制,并且行结束
处理自己,根据您喜欢的任何约定。
同样,如果文件实际上是一个套接字,传递
与另一台机器,你会想要打开它的二进制,和
处理行结束自己,根据要求
协议。


This may properly belong to a different part of Stack Exchange but I don't think so - programmers.se is more about other things.

Getting to the question: There are things you can do with std::ios::binary that you cannot do in text mode (E.g. relative seek) but I cannot find anything to do in text mode that you cannot do in binary mode - even reading the file as text with e.g. std::getline()

So why would I ever open as text? As a perhaps-related question, why not open as binary by default? Whose use-case does that break?

EDIT Additional information

Here's what's causing me to ask:

I have a file which is created on a windows system - that is, the line-endings are CR LF.

I am opening it with std::ifstream using the std::ios::binary flag

I am parsing through the file with std::getline and getting exactly the behavior I would expect - getline reads one line at a time.

System: Windows 7 Pro

Compiler: g++ for MINGW32

解决方案

What can you do in text mode that you can't do in binary? Read text, for starters. A file opened in text mode automatically translates between the '\n' character internally, and whatever the system uses to delimit lines in files externally. It can also recognize an arbitrary end of file, even when the underlying system requires file sizes to be a multiple of some fixed size.

The choice today is somewhat complicated by the fact that you often have to access the files from incompatible systems. If you have a file system mounted on both Windows and Unix, write it as text under Windows, and read it as text under Unix, then you'll see extra characters. In such cases, it may be preferable to read and write binary, and to do the line end handling yourself, according to whatever conventions you prefer. Similarly, if the "file" is actually a socket, communicating with another machine, you'll want to open it in binary, and handle line endings yourself, according to the requirements of the protocol.

这篇关于为什么我会打开一个文件(std :: ifstream)没有std :: ios :: binary?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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