文件vs fstream [英] FILE vs fstream

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

问题描述


可能存在重复:

std :: ifstream明显慢于文件?

你在C ++代码中使用了哪个I / O库?


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



认为是FILE比fstream更有效。

解决方案

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



非常倾向于C ++ ,反C的人可能会告诉你一些沿着 fstream 的行可以更轻松地处理不同的类型。有了 FILE ,你有两个选择 - 以字节为单位或者处理格式字符串。由于 printf fwrite 等。不知道他们的论点是什么真正的类型,这使得更容易搞砸。还有一个事实是,一个C ++类将有一个析构函数,所以当对象超出范围时,你可以免费地进行清理。 (虽然...你真的想要像 fflush 这样的东西静静地在析构函数中发生吗?可能不会)。对于这样的论点,我会说这不是真的> 使用 FILE 是一个负担,但是,有些人比我更强烈地感觉到这些问题。



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



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

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

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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