passing files to functions

I'm trying to write to a file using pass by reference. My first time doing this. I don't think the file is being created though("ShapesFile.txt") and I'm not sure where it's gone wrong. It is compiling ok and writing to the console ok, but not to the file(I don't think cause I can't find it). I removed the flower boxes to shorten code. Any ideas?

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <string>
#include <iomanip>
#include <istream>
#include <math.h>
#include <fstream>

using namespace std;

//prototypes
double Area_Circle(int&,double&);
int Area_Rectangle(int&,int&,double&);
int Area_Square(int&,double&);
void Menu(int&);
void Input_Side(int&);
void Output_Rectangle(int&,int&,double&);
void Output_Shape(int&,double&);
bool CheckFiles(fstream&,string); 
void OutFileShapes1(fstream&,int&,int&,double&);
void OutFileShapes2(fstream&,int&,double&);


int main()
 {  
	char filename[]= "E:ShapesFile.txt";
	int radius, width, length = 0, side, choice; 
	double area;
	char answer;
	fstream shapesFileout;
	shapesFileout.open(filename,ios::out);
    
    if(CheckFiles(shapesFileout,filename))
    {
      
        do 
    	{
        	Menu(choice); 
        	
        	switch(choice)
        	{
        	case 1: 
                cout << "\nEnter the width: ";
                Input_Side(width); 
                cout << "\nEnter the length: ";
                Input_Side(length); 
        		Area_Rectangle(width,length,area); 
        		cout << " \n Rectangle:\t" << "Width\t" << "Length\t" << "Area\n";
        		Output_Rectangle(width,length,area); 
        		OutFileShapes1(shapesFileout,width,length,area);
        		break;
        	case 2: 
                cout << "\n Enter the radius: ";
                Input_Side(radius); 
        		Area_Circle(radius,area); 
        		cout << " \n Circle:\t" << "Radius\t" << "Area\n"; 
        		Output_Shape(radius,area); 
        		OutFileShapes2(shapesFileout,radius,area);
        		break;
        	case 3: 
                cout << "\n Enter the length of side: ";
                Input_Side(side);
        		Area_Square(side,area); 
        		cout << " \n Square:\t" << "Sides\t" << "Area\n";
        		Output_Shape(side,area); 
        		OutFileShapes2(shapesFileout,side,area);
        	    break;
            case 4: 
                cout << " Have a nice day!" << endl << endl; 
                break;
            //validation if non-menu items are entered.  
        	default: cout << "\n That was an invalid choice." << endl << endl;
            }
             
            cout << " Would you like to run the program again? (Y or N) ";
            cin >> answer;
            cout << endl; 
        // loop to prompt user to run program again     
    	}while(answer == 'Y' || answer == 'y'); 
    }
	else
         cout << "File open error!" << endl;
	
    cout << " Have a nice day!" << endl << endl;
	
    system("PAUSE");
    return EXIT_SUCCESS;

}


    void Menu(int &choice) 
    { 
         cout << "\n Choose a shape to calculate AREA";
	     cout << "\n...................................";
         cout << "\n 1- Rectangle\n 2- Circle\n 3- Square\n 4- Exit";
	     cout << "\n\n Enter your choice: ";
	     cin >> choice; 
    } 
    

    
    void Input_Side(int &side) 
    {  
		cin >> side; 	
    }
    
 
	
    double Area_Circle(int &radius, double &area) 
	{ 
         area = (3.14 * radius * radius); 
	} 
	

 
	int Area_Rectangle(int &length, int &width, double &area) 
	{ 
         area = (length * width); 
	} 
	

 
	int Area_Square(int &side, double &area)
	{ 
         area = (side * side); 
	} 
	

	
    void Output_Rectangle(int &width, int &length, double &area)
    { 
        cout << "\t\t" << width << "\t" << length << "\t" << area;
        cout << endl << endl;
    }
    
 
 
    void Output_Shape(int &side, double &area)
    { 
        cout << "\t\t" << side << "\t" << area;
        cout << endl << endl;
    } 
	

	
    bool CheckFiles(fstream& ShapesFile, string filename)
    { 
        ShapesFile.open(filename.c_str(), ios::in);
        if (ShapesFile.fail())
            return false;
        else
            return true;
    }
     

	
    void OutFileShapes1(fstream &ShapesFile,int &width, int &length, double &area)
    { 
        ShapesFile << setw(8) << width << setw(8) << length << setw(8) << area;
    }


	
    void OutFileShapes2(fstream &ShapesFile, int &side, double &area)
    { 
        ShapesFile << setw(16) << side << setw(8) << area;
    }
char filename[]= "E:ShapesFile.txt"; should be char filename[]= "E:\\ShapesFile.txt";
thanks MiiNiPaa. I tried it, but I still don't see the file in my E: drive. :(
I tried removing the drive altogether and just allowing it to save in the same folder as the source code, but I don't see it there either.
If a new file is to be always created:
1
2
3
fstream shapesFileout;
// shapesFileout.open(filename,ios::out); 
shapesFileout.open( filename, ios::out|ios::trunc );


Or simpler:
std::ofstream shapesFileout( filename ) ;

I see. Good thought! That still doesn't solve my issue of where in the world the file is. :) I searched everywhere and can't find it. I'm not sure what else to do here.
Are you able to create a file with this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <fstream>

int main()
{
    const char* const path = "E:\\ShapesFile.txt" ;
    std::ofstream file( path ) ;
    if( file ) 
   {
         std::cout << "file is created\n" ;
         if( file << "hello world\n" ) 
              std::cout << "file can be written into\n" ; 
   }
}

yes this worked for me. I've never seen const char* const path =

should I change the line to that? or are you thinking something else?
I don't think that is the problem.
Did you try using std::ofstream in your program?
Ok. I changed to ifstream and ofstream and got rid of fstream. I just can't get that to work for me. :(
Now, this is compiling, but I still can't find ShapesFile.txt anywhere? This is still kicking my a$$! Any more ideas?


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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <iostream>
#include <string>
#include <iomanip>
#include <istream>
#include <math.h>
#include <fstream>

using namespace std;

//prototypes
double Area_Circle(int&,double&);
int Area_Rectangle(int&,int&,double&);
int Area_Square(int&,double&);
void Menu(int&);
void Input_Side(int&);
void Output_Rectangle(int&,int&,double&);
void Output_Shape(int&,double&);
bool CheckFiles(ofstream&,string); 
bool CheckFiles(ifstream&,string);
void OutFileShapes1(ofstream&,int&,int&,double&);
void OutFileShapes2(ofstream&,int&,double&);


int main()
 {  
	char filename[]= "ShapesFile.txt";
	int radius, width, length = 0, side, choice; 
	double area;
	char answer;
	ofstream shapesFileOut;
	ifstream shapesFileIn;
    shapesFileOut.open(filename);
    shapesFileIn.open(filename);
    
    if(CheckFiles(shapesFileOut,filename) && CheckFiles(shapesFileIn,filename))
    {
      
        do 
    	{
        	Menu(choice); 
        	
        	switch(choice)
        	{
        	case 1: 
                cout << "\nEnter the width: ";
                Input_Side(width); 
                cout << "\nEnter the length: ";
                Input_Side(length); 
        		Area_Rectangle(width,length,area); 
        		cout << " \n Rectangle:\t" << "Width\t" << "Length\t" << "Area\n";
        		Output_Rectangle(width,length,area); 
        		OutFileShapes1(shapesFileOut,width,length,area);
        		break;
        	case 2: 
                cout << "\n Enter the radius: ";
                Input_Side(radius); 
        		Area_Circle(radius,area); 
        		cout << " \n Circle:\t" << "Radius\t" << "Area\n"; 
        		Output_Shape(radius,area); 
        		OutFileShapes2(shapesFileOut,radius,area);
        		break;
        	case 3: 
                cout << "\n Enter the length of side: ";
                Input_Side(side);
        		Area_Square(side,area); 
        		cout << " \n Square:\t" << "Sides\t" << "Area\n";
        		Output_Shape(side,area); 
        		OutFileShapes2(shapesFileOut,side,area);
        	    break;
            case 4: 
                cout << " Have a nice day!" << endl << endl; 
                break;
            //validation if non-menu items are entered.  
        	default: cout << "\n That was an invalid choice." << endl << endl;
            }
             
            cout << " Would you like to run the program again? (Y or N) ";
            cin >> answer;
            cout << endl; 
        // loop to prompt user to run program again     
    	}while(answer == 'Y' || answer == 'y'); 
    }
	else
         cout << "File open error!" << endl;
	
    cout << " Have a nice day!" << endl << endl;
	
    system("PAUSE");
    return EXIT_SUCCESS;

}

void Menu(int &choice) 
    { 
         cout << "\n Choose a shape to calculate AREA";
	     cout << "\n...................................";
         cout << "\n 1- Rectangle\n 2- Circle\n 3- Square\n 4- Exit";
	     cout << "\n\n Enter your choice: ";
	     cin >> choice; 
    } 
    

    
    void Input_Side(int &side) 
    {  
		cin >> side; 	
    }
    
  
	
    double Area_Circle(int &radius, double &area) 
     { 
         area = (3.14 * radius * radius); 
     } 
	

 
    int Area_Rectangle(int &length, int &width, double &area) 
     { 
         area = (length * width); 
     } 
	

 
    int Area_Square(int &side, double &area)
    { 
         area = (side * side); 
    } 
	

	
    void Output_Rectangle(int &width, int &length, double &area)
    { 
        cout << "\t\t" << width << "\t" << length << "\t" << area;
        cout << endl << endl;
    }
    

 
    void Output_Shape(int &side, double &area)
    { 
        cout << "\t\t" << side << "\t" << area;
        cout << endl << endl;
    } 
	

	
    bool CheckFiles(ofstream& ShapesFile, string filename)
    { 
        ShapesFile.open(filename.c_str(), ios::out);
        if (ShapesFile.fail())
            return false;
        else
            return true;
    }
     

	
    bool CheckFiles(ifstream& ShapesFile, string filename)
    { 
        ShapesFile.open(filename.c_str(), ios::out);
        if (ShapesFile.fail())
            return false;
        else
            return true;
    }     
     

	
    void OutFileShapes1(ofstream &ShapesFile,int &width, int &length, double &area)
    { 
        ShapesFile << setw(8) << width << setw(8) << length << setw(8) << area;
    }



	
    void OutFileShapes2(ofstream &ShapesFile, int &side, double &area)
    { 
        ShapesFile << setw(16) << side << setw(8) << area;
    }
I just realized I forgot the backslashes "E:\\ShapesFile.txt" but that still didnt' work. Help!
I'm not sure, but you are opening the same file once in Write mode and again in Read mode. You should not do this.

You should open it in write mode only when you need to write to it, and open it in read mode only when you need to read from it.

Opening the same file in two different ways is probably bugging something there.
should I not open the file to write until I get to the function then? instead of declaring the streams in main? I'll try it that way to see if that helps. I think that's what you are referring to.
Yep, that's what I meant. Give it a try.
Hi EssGeEich,
unfortunately I just remembered the assignment is to pass files by reference to the functions, so I can't do that. I have to open them, then pass the pointer to write to them in the function.
This is compiling ok, but I can't find the file anywhere on my computer!

I'm only working on writing to the file at the moment. I need to work on output formatting, but can't even find it. Eventually I will have to read from the file in another function, but I can't even begin to work on that part either until I can find the file.

I need your brains please! Help! Why can't I locate it? and if it's not opening correctly, why is the fail message not working? I changed everything from 'fstream' to 'ofstream' to see if that helped, but it didn't.



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <string>
#include <iomanip>
#include <istream>
#include <math.h>
#include <fstream>

using namespace std;

//prototypes
void Area_Circle(int&,double&);
void Area_Rectangle(int&,int&,double&);
void Area_Square(int&,double&);
void Menu(int&);
void Input_Side(int&);
void Display_Rectangle(int&,int&,double&);
void Display_Shape(int&,double&);
void Output_Rectangle(ofstream&,int&,int&,double&);
void Output_Shape(ofstream&,int&,double&);


int main()
 {  
    char filename[]= "shapes.txt";
    int radius, width, length = 0, side, choice; 
    double area;
    char answer;
    ofstream shapesFile;
    shapesFile.open(filename);
    //shapesFile.open(filename, ios::in | ios::out);
	
    if(shapesFile.fail())
	{
	    shapesFile.open(filename);
	    //shapesFile.open(filename, ios::in | ios::out);
	}
	
    do 
  	{
       	Menu(choice); 
       	switch(choice)
       	{
       	case 1: 
                cout << "\nEnter the width: ";
                Input_Side(width); 
                cout << "\nEnter the length: ";
                Input_Side(length); 
       		    Area_Rectangle(width,length,area); 
        	    cout << " \n Rectangle:\t" << "Width\t" << "Length\t" << "Area\n";
        	    Display_Rectangle(width,length,area);
			    shapesFile << " \n Rectangle:\t" << "Width\t" << "Length\t" << "Area\n";
        	    Output_Rectangle(shapesFile,width,length,area); 
			    break;
        case 2: 
                cout << "\n Enter the radius: ";
                Input_Side(radius); 
        		Area_Circle(radius,area); 
        		cout << " \n Circle:\t" << "Radius\t" << "Area\n";
                Display_Shape(side,area); 
				shapesFile << " \n Circle:\t" << "Radius\t" << "Area\n"; 				
        		Output_Shape(shapesFile,radius,area);
        		break;
        case 3: 
                cout << "\n Enter the length of side: ";
                Input_Side(side);
        		Area_Square(side,area); 
        		cout << " \n Square:\t" << "Sides\t" << "Area\n";
        		Display_Shape(side,area);
				shapesFile << " \n Square:\t" << "Sides\t" << "Area\n";
        		Output_Shape(shapesFile,side,area); 
        	    break;
       case 4: 
                cout << " Have a nice day!" << endl << endl; 
                break;
              
       default: cout << "\n That was an invalid choice." << endl << endl;
       }
       cout << " Would you like to run the program again? (Y or N) ";
       cin >> answer;
       cout << endl; 
            
    }while(answer == 'Y' || answer == 'y'); 
    cout << " Have a nice day!" << endl << endl;
	shapesFile.close();
	
    system("PAUSE");
    return EXIT_SUCCESS;

}


    void Menu(int &choice) 
    { 
         cout << "\n Choose a shape to calculate AREA";
	     cout << "\n...................................";
         cout << "\n 1- Rectangle\n 2- Circle\n 3- Square\n 4- Exit";
	     cout << "\n\n Enter your choice: ";
	     cin >> choice; 
    } 
    

    
    void Input_Side(int &side) 
    {  
		cin >> side; 	
    }
    
  
	
    void Area_Circle(int &radius, double &area) 
	{ 
         area = (3.14 * radius * radius); 		
	} 
	

 
	void Area_Rectangle(int &length, int &width, double &area) 
	{ 
         area = (length * width); 		
	} 
	

 
	void Area_Square(int &side, double &area)
	{ 
         area = (side * side); 		
	} 


	
    void Display_Rectangle(int &width, int &length, double &area)
    { 
        cout << "\t\t" << width << "\t" << length << "\t" << area;
        cout << endl << endl;
    }
    

 
    void Display_Shape(int &side, double &area)
    { 
        cout << "\t\t" << side << "\t" << area;
        cout << endl << endl;
    }
	

	
    void Output_Rectangle(ofstream &file,int &width, int &length, double &area)
    { 
        file << "\t\t" << width << "\t" << length << "\t" << area;
        file << endl << endl;
    }
    

 
    void Output_Shape(ofstream &file,int &side, double &area)
    { 
        file << "\t\t" << side << "\t" << area;
        file << endl << endl;
    }
I moved the source code to the E: drive and now I can see the file is being created there with the source file. Not sure if something was wrong with this source code being in the C: in the compiler directory, but whatever. It's working. Now to formatting and inputting!
Not sure if something was wrong with this source code being in the C: in the compiler directory
Windows security politics?
I confirm.
Unless you run your program as administrator, your program isn't fully able to manage important system directories like:

1. Your Windows System Drive (C: for you)
2. Your Program Files folder (C:\Program Files\ and similar)
3. Your Windows folder (C:\Windows probably)

Also, depending on your settings, you may not be able to access more folders who are protected.

This should only apply from Windows Vista, I don't think Windows XP has such restrictions on by default.
Topic archived. No new replies allowed.