Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,169 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,913 people online right now. Registration is fast and FREE... Join Now!




Difference Between fstream class's

 
Reply to this topicStart new topic

Difference Between fstream class's, ifstream, fstream, ofstream

lockdown
19 Dec, 2007 - 03:01 PM
Post #1

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
So I am reading threw some reference material and notice that I did not learn 1 other fstream class during into to C++.

I learned ifstream is for input and ofstream is for output to files. But their is fstream that dose both read and write data to files. So what is the difference between using fstream or ifstream to write data to a file?
User is offlineProfile CardPM
+Quote Post

Bench
RE: Difference Between Fstream Class's
19 Dec, 2007 - 04:25 PM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 617



Thanked: 14 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
To start with, you can't use ifstream to write to files. And this is exactly why you might want to use it. One of the best ways to keep others who use your code from doing something stupid (like using an input stream to write to a file), is to impose a technical restriction which prevents them from doing so.
User is offlineProfile CardPM
+Quote Post

lockdown
RE: Difference Between Fstream Class's
19 Dec, 2007 - 05:34 PM
Post #3

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
So its better to uses ifstream and ofstream because it prevents errors and mistakes from the user?
User is offlineProfile CardPM
+Quote Post

Bench
RE: Difference Between Fstream Class's
20 Dec, 2007 - 04:05 AM
Post #4

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 617



Thanked: 14 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
Not from the user, but from others who use your code (or yourself, perhaps after a prolonged period of time away from that code). For example, if you wrote a function whose sole purpose was to read a line of data from a file, parse that line, and then return an object, you'd want to make sure that the caller hadn't sent an fstream which had been opened for output. The best way to do that is to force the caller to provide an ifstream instead.

This post has been edited by Bench: 20 Dec, 2007 - 04:06 AM
User is offlineProfile CardPM
+Quote Post

lockdown
RE: Difference Between Fstream Class's
20 Dec, 2007 - 05:17 AM
Post #5

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
O ok yeah that makes sense.

Thanks for clearing that up.
User is offlineProfile CardPM
+Quote Post

Lizardking
RE: Difference Between Fstream Class's
21 Apr, 2008 - 12:53 PM
Post #6

New D.I.C Head
*

Joined: 1 Apr, 2008
Posts: 10


My Contributions
ifstream and ofstream. Do you manually need to make a file lets say ("infile.dat") do you actually need to make the file? or does the code do it for you? and if you do what exactly must you do to make your code work with Fstream.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:29AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month