fstream.open() or fstream.close() don't affect, file is always open

what is the problem with this code files are always open EVEN BEFORE using fstream.open() and EVEN AFTER using fstream.open()

What is the problem in here?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//headers----------------
#include <iostream>
#include <fstream>
#include <stdlib.h>
//----------------headers

using namespace std;

void f1(ofstream *);
void f2(ofstream *);

main(){

//variables-----------
const char file[] = "Struct.txt";
ofstream ofh;
ifstream ifh;
//-----------variables


//main code------------

if(ofh) cout << "out File is still open" << endl; //I haven't used ofh.open()
                                         //yet but file open message is shown
if(ifh) cout << "in File is still open" << endl; //This one too is showing 
//cout message on console I haven't even told file name yet to object ifh

cout << "-----------------" << endl << endl;

ofh.open(file, ios::app);
ifh.open(file, ios::in);

f1(& ofh);
f2(& ofh);

cout << "Closing" << endl;

ofh.close();
ifh.close();

if(ofh) cout << "out File is still open" << endl; //Even after ofh.close()
//message shows up
if(ifh) cout << "in File is still open" << endl; //This one too
//-----------main code

cout << endl << "Program end" << endl;
system("pause");

       }//---------main()
       
void f1(ofstream * ofp){
     
     if(!ofp){
          
          cout << "f1 failed" << endl;
          
          }else{
                
                cout << "f1 passed" << endl;
                
                }//------else-if
     
     }//-------f1()
     
void f2(ofstream * ofp){
     
     if(!ofp){
          
          cout << "f2 failed" << endl;
          
          }else{
                
                cout << "f2 passed" << endl;
                
                }//------else-if
     
     }//-------f2()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>

int main()
{
    {
        std::ifstream file ; // default constructor 
        std::cout << std::boolalpha 
                  << "the stream is in a failed state: " << file.fail() << '\n' // false
                  << "the stream is not in a failed state: " << bool(file) << '\n' // true
                  << "the stream is open: " << file.is_open() << '\n' ; // false
    }
    std::cout << "-----------------------------------\n" ;
    { 
        std::ifstream file( "bad_path" ) ; // constructor attempts to open the file
        std::cout << std::boolalpha 
                  << "the stream is in a failed state: " << file.fail() << '\n' // true
                  << "the stream is not in a failed state: " << bool(file) << '\n' // false
                  << "the stream is open: " << file.is_open() << '\n' ; // false
    }
}

http://coliru.stacked-crooked.com/a/d7bee0a7ba879bb0
JLBorges I don't get what you wanna show with this code well let me rephrase my question

I think
1. when we declare a variable of type ifstream xyz; the xyz should not be in open state

I check open state by " if(xyz) cout << "File is open" << endl;

but its not like that just declaration of ifstream variable opens it too even though it doesn't even know the file name
What if you check for open state by using is_open() ?

http://www.cplusplus.com/reference/fstream/ifstream/is_open/
Oh I'm so sorry guys I didn't know about is_open() being the actual file open test

Well now I'm working further on my program

Thanks all of you
Topic archived. No new replies allowed.