I/O question

Nov 9, 2014 at 4:10pm
Ok simple question I hope. I read your tutorial on I/O streams and just had a couple questions between --> http://www.cplusplus.com/doc/tutorial/files/

 
	if (arbitrary.is_open()) 


and

 
	if (arbitrary.open()) 


I am taking a class right now and the teacher is wanting us to utilize the .fail function. However when I was reading your article I noticed the .is_open and in the tutorial it says it checks to see if the file is actually running, and returns a bool true value or false. I guess my basic question is does this do basically do the same thing as running and .fail function. Because both are able to have outputs saying the "program failed". Or am I reading this wrong and the is_open will just passively fail
Last edited on Nov 9, 2014 at 4:12pm
Nov 9, 2014 at 4:23pm
closed account (SECMoG1T)
 
if (arbitrary.fail ()) /*is equivalent to */ if (! arbitrary.open ()) /* and*/ if (! arbitrary. is_open ())


Bur I would use them for different purpose

I would use fail () or open () to check if a file successfully opened
I would use is_open () in iterations e.g. on reading the file

1
2
while (infile.is_open ())
/// read the file 
Last edited on Nov 9, 2014 at 4:35pm
Nov 9, 2014 at 4:28pm
so if (arbitrary.is_open()) /*is equivalent to */ arbitrary.open /*if there are no failures? */
Last edited on Nov 9, 2014 at 4:28pm
Nov 9, 2014 at 4:47pm
closed account (SECMoG1T)
Yea
1
2
 if (arbitrary.is_open ())   /*and */ if (arbitrary.open ()) /// are equal 
        /// both return true if the file is open and false if the file isn't open  
Nov 10, 2014 at 1:36am
I guess what I am cunfused on is how they both return false. do they just passively in the background do nothing or does one output have a different outcome?
Topic archived. No new replies allowed.