本文介绍了文件vs fstream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



认为是FILE比fstream更有效。

解决方案

一个是C,一个是C ++。番茄,番茄。 (当你写出来的时候,这个表达式的效果不是很好。)我的猜测是,你不可能看到性能差异。



非常倾向于C ++ ,反C的人可能会告诉你一些沿着使用 FILE 是一个负担,但是,有些人比我更强烈地感觉到这些问题。



最后它会归结为你的应用程序试图做什么,可能是 FILE , fstream ,或者两者都可以满足你的需求。



选择什么是有效的,对其他人选择是否灵活,理解论据,不要太宗教关于它。这是我的建议。 : - )

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

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

解决方案

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.

A very C++ inclined, anti-C person will probably tell you something along the lines of fstream being able to deal with differing types with more ease. With FILE you have two options -- deal in bytes or deal in format strings. Since printf or fwrite et 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 fflush to silently happen in a destructor? Probably not.) To these sorts of arguments I would say that it's not really that much of a burden to use FILE, but hey, some people feel more strongly than I on these matters.

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.

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. :-)

这篇关于文件vs fstream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 01:08