Jump to content
Gord P

TFileStream vs ifstream/ofstream

Recommended Posts

I am updated an older C++Builder program.  In it I use ifstream and ofstream for reading and writing text files, which then get parsed to extract various pieces of data.

 

It works well enough but I wonder if I should be using TFileStream going forward.  What are the advantages, if any, of using TFileStream?

Share this post


Link to post

I came across this article by Yilmaz Yoru on the Embarcadero C++Builder Community site:

How To Read And Write Text Files In A Modern C++ App - March 28, 2022

In the article he only talks about ofstream and ifstream.  No mention of TFileStream.

 

However, in an article two years earlier (Windows File Operations in Modern C++) he does seem to promote TFileStream.

 

I guess there is no real must-do way.  I suppose sticking with the fstream methods is more portable.

 

 

Share this post


Link to post

"Which is the best way?" questions in C++ are always tricky! The language has evolved so much (and continues to do so). We C++ Builder users have the added complication of the use of the VCL or FireMonkey functions (that come from the Delphi RTL). How important it is to keep your code portable is certainly one question to consider. Re: text files : also take into account encoding possiblities - if you need to anticipate different encodings then the VCL support is often the easiest route. I tend to use TStringList as the store and then the LoadFromFile() and SaveToFile() functions.

  • Like 1

Share this post


Link to post

Yes I recognize asking which is the best way can be tricky and subjective.  I was just trying to make sure I wasn't using something that was generally considered obsolete, and as far as I can tell it isn't.  Thanks for your comment and good point on encoding.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×