C++ 文件与 fstream

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5570361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:26:02  来源:igfitidea点击:

FILE vs fstream

c++file-io

提问by Vineet G

Possible Duplicates:
Is std::ifstream significantly slower than FILE?
Which I/O library do you use in your C++ code?

可能的重复:
std::ifstream 是否比 FILE 慢得多?
您在 C++ 代码中使用哪个 I/O 库?

I was wondering what are the pros or cons of using fstream over FILE in C++?

我想知道在 C++ 中使用 fstream 而不是 FILE 的优缺点是什么?

The one pro I think is that FILE is more efficient than fstream.

我认为的一个专业人士是 FILE 比 fstream 更有效。

回答by asveikau

One is C and one is C++. Tomato, tomato. (That expression doesn't work nearly as well when you write it out.) My guess is that you are not likely to see a performance difference.

一种是C,一种是C++。番茄,番茄。(当你写出来时,这个表达式几乎没有效果。)我的猜测是你不太可能看到性能差异。

A very C++ inclined, anti-C person will probably tell you something along the lines of fstreambeing able to deal with differing types with more ease. With FILEyou have two options -- deal in bytes or deal in format strings. Since printfor fwriteet al. don't know what the "real" type of their arguments are this makes it easier to screw up. There's also the fact that a C++ class will have a destructor and so you get cleanup "for free" when the object goes out of scope. (Although... Do you really want something like fflushto silently happen in a destructor? Probably not.) To these sorts of arguments I would say that it's not really thatmuch of a burden to use FILE, but hey, some people feel more strongly than I on these matters.

一个非常喜欢 C++、反 C 的人可能会告诉你一些关于fstream能够更轻松地处理不同类型的东西。有了FILE你有两个选择-交易中的字节或交易的格式字符串。由于printffwrite等。不知道他们的论点的“真实”类型是什么,这使得更容易搞砸。还有一个事实是,C++ 类将有一个析构函数,因此当对象超出范围时,您可以“免费”进行清理。(虽然...你真的要像fflush在析构函数默默发生吗?也许不是)。为了这些各种各样的论据我会说,这是不是真的太大的负担,使用FILE,但嘿,有些人感觉更强烈比我在这些事情上。

Eventually it will boil down to what exactly your application is trying to do, and it may be that FILE, fstream, or both can adequately suit your needs.

最终将归结到正是您的应用程序正在试图做的,它可能是FILEfstream或两者能充分满足您的需求。

Pick what works, be flexible with what other people choose, understand the arguments and don't get too religious about it. That's my advice. :-)

选择有效的方法,灵活对待其他人的选择,理解论点并且不要过于虔诚。这就是我的建议。:-)

回答by Jeffrey Faust

  • fstream is a better encapsulation and has higher level concepts.
  • fstream is exception safe.
  • fstream is also a stream and can be treated generically as a stream.
  • fstream 是更好的封装,具有更高层次的概念。
  • fstream 是异常安全的。
  • fstream 也是一个流,可以被一般地视为流。

Imagine:

想象:

void read(istream& istr)

无效读取(istream&istr)

We could pass in an ifstream, a istrstream, or even cin. This is very useful for unit testing.

我们可以传入 ifstream、istrstream 甚至 cin。这对于单元测试非常有用。

回答by octal9

std::fstreamis typesafe, has internationalization support and is (warning: opinion) easier to use.

std::fstream是类型安全的,具有国际化支持并且(警告:意见)更易于使用。

When a std::fstreamgoes out of scope it is destructed for you, regardless of whether you forgot to fstream::close()it.

当 astd::fstream超出范围时,它会为您销毁,无论您是否忘记了fstream::close()它。