fstream will not work properly?

Hi! Please help me. I am trying to write a program for school. I have been working on it for a while now. I am trying to use the fstream library to create a file, output user-input data to it, and then close the file. Unfortunately, I declare the file and open it in one function. I am then trying to output data to it and close it in another. Unfortunately, my compiler keeps telling me that "'Open_File' was not declared in this scope."


'Open_File' is what I am declaring the file as when I open it with fstream. I have tried to include the opening and closing and outputting of the file in the same function also, however, I keep getting the same error!

Does anyone know what I might be doing wrong?
I think you're not declaring your output file properly. Post the code for me?
Hi Ciphermagi:

This is the code with everything in one function:

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
//Preprocessor Directives
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>

using namespace std;


//Functions
string New_File();


//Main Function Heading
int main () {
    

	//Declaration Section
	int Decide;
	char File_Name[81];
	string Random;
	
	//Statements
	cout << endl << endl << "Hello! Would you like to create a new file, or open a pre-existing one? Press '1' to create a new file, or '2' to open one.";
	cin >> Decide;
	
	if (Decide == 1) {
		
		cout << endl << endl << endl << "What would you like to name the new file? Please do not use any special symbols.\n";
		cin.getline(File_Name, 80);
		
		
		fstream Open_File;
		Open_File.open(File_Name);
		
		
	}
	
	else if (Decide == 2) {
		
		cout << endl << endl << endl << "What is the name of the file you would like to open? Please type it EXACTLY as it is spelled.";
		cin.getline(File_Name, 80);
		
		
		fstream Open_File;
		Open_File.open(File_Name, ios::ate | ios::app);
		
	}
	
	cout << endl << endl << "Please enter a random string.";
	cin	>> Random;
	
	
	cout << endl << endl << Random;
	cout << endl << endl << "This file will be saved to the current program 'debug' dirrectory.";
	
	Open_File << Random;
	
	Open_File.close();
	
	return 0;
	
	
}



THIS is the code with multiple functions:


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
//Preprocessor Directives
#include <iostream>
#include <iomanip.h>
#include <string.h>
#include <stdio.h>
#include <fstream>
#include <vector>

using namespace std;


//Functions
string New_File();


//Main Function Heading
int main () {
    
	//Statements
	
	New_File();
	
	cout << endl << endl << Random;
	cout << endl << endl << "This file will be saved to the current program 'debug' dirrectory.";
	
	Open_File << Random;
	
	Open_File.close();
	
	return 0;
	
	
}


//The Below Function Creates or Opens a New or Existing File:
string New_File() {
	
	//Declaration Section
	int Decide;
	char File_Name[81];
	string Random;
	
	//Statements
	cout << endl << endl << "Hello! Would you like to create a new file, or open a pre-existing one? Press '1' to create a new file, or '2' to open one.";
	cin >> Decide;
	
	if (Decide == 1) {
		
		cout << endl << endl << endl << "What would you like to name the new file? Please do not use any special symbols.\n";
		cin.getline(File_Name, 80);
		
		
		fstream Open_File;
		Open_File.open(File_Name);
		
		
	}
	
	else if (Decide == 2) {
		
		cout << endl << endl << endl << "What is the name of the file you would like to open? Please type it EXACTLY as it is spelled.";
		cin.getline(File_Name, 80);
		

		fstream Open_File;
		Open_File.open(File_Name, ios::ate | ios::app);
		
	}
	
	cout << endl << endl << "Please enter a random string.";
	cin	>> Random;
	
	return Random;
	
}
I just tried defining:

1
2
fstream Open_File;
Open_File.open("Hello");


This appeared to work, so I am wondering if maybe the issue is the "if-else/if" statement?

Any help is appreciated!
Try this:
1
2
ofstream Open_File;
Open_File.open(File_Name, ios::ate | ios::app);


EDIT: Just as an aside, I would use a switch with three cases, case 1, case 2, and default. That will prevent the instance where nothing at all happens in case someone puts in an invalid selection, and give you a chance to restart the input sequence.
Last edited on
No... Unfortunately, that didn't fix the issue...
Okay, I might have it...you're passing a pointer to an array as the filename. File_Name should be a string.
OOHHH.... ok. Let me try that
your file is only opened inside the curly braces, so process it there, ditto in the function
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
string New_File() {	
	//Declaration Section
	int Decide;
	string Random, File_Name;
         ofstream Open_File;
	//Statements
	cout << endl << endl << "Hello! Would you like to create a new file, or open a pre-existing one? Press '1' to create a new file, or '2' to open one.";
	cin >> Decide;	
	switch(Decide){
		case 1:	
			cout << endl << endl << endl << "What would you like to name the new file? Please do not use any special symbols.\n";
			cin.getline(File_Name, 80);		
			Open_File.open(File_Name);
			break;
		case 2:		
			cout << endl << endl << endl << "What is the name of the file you would like to open? Please type it EXACTLY as it is spelled.";
			cin.getline(File_Name, 80);
			Open_File.open(File_Name, ios::ate | ios::app);
			break;
		default:
			cout << "That is not a valid option." << endl;
			New_File();
			break;		
	}	
	cout << endl << endl << "Please enter a random string.";
	cin >> Random;	
	return Random;	
}


Try this one out.

EDIT: Move the declarationofstream Open_File; out of the switch, otherwise your compiler will throw errors about declaring something across a switch.
Last edited on
OOOOOHHHH!!!! That makes so much sense! Thanks Matsom!

Thanks Ciphermagi too!




HOWEVER:

I managed to get it to work by including the entire process inside a single function, however, the file isn't actually created inside the directory from which the program is being run. Isn't this where it is supposed to be created? It doesn't show up.
The program appears to work, however, it doesn't actually? I'm confused. This is my new code:


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
//Preprocessor Directives
#include <iostream>
#include <iomanip.h>
#include <string.h>
#include <stdio.h>
#include <fstream>
#include <vector>

using namespace std;


//Functions
string New_File();


//Main Function Heading
int main () {

	//Declaration Section
	char File_Name[81];
	string Random;
	
	//Statements		
	
	cout << endl << endl << endl << "What is the name of the file you would like to open/create? Please type it EXACTLY as it is spelled.";
	cin.getline(File_Name, 80);
	
	
	
	fstream Open_File;
	Open_File.open("File_Name", ios::ate | ios::app);
	
	
	
	cout << endl << endl << "Please enter a random string.";
	cin >> Random;
	
	
	
	
	cout << endl << endl << Random;
	cout << endl << endl << "This file will be saved to the current program 'debug' dirrectory.";
	
	Open_File << Random;
	
	Open_File.close();
	
	return 0;
	
	
}



The program runs successfully, yet the file doesn't seem to save. I can't find it?
that depends on your IDE's project settings, e.g in Code::Blocks you can find it in your 'main' directory not the Debug directory, if you're using default settings. To change it go to {Project->Properties->Build Targets->Execution working dir.}
I changed your code to the following:

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
//Preprocessor Directives
#include <iostream>
#include <iomanip.h>
#include <string.h>
#include <stdio.h>
#include <fstream>
#include <vector>

using namespace std;


//Functions
string New_File();


//Main Function Heading
int main () {

	//Declaration Section
	string Random, File_Name;
	fstream Open_File;
	//Statements		
	cout << endl << endl << endl << "What is the name of the file you would like to open/create? Please type it EXACTLY as it is spelled.";
	cin.getline(File_Name, 80);
	Open_File.open(File_Name, ios::ate | ios::app);
	cout << endl << endl << "Please enter a random string.";
	cin >> Random;
	cout << endl << endl << Random;
	cout << endl << endl << "This file will be saved to the current program 'debug' dirrectory.";
	Open_File << Random;
	Open_File.close();
	return 0;
}


You were using a literal string instead of the variable name to open the file:
Open_File.open("File_Name", ios::ate | ios::app);
so look for a file named "File_Name" in your directory.
I also changed File_Name to a string type variable instead of an array of characters.
Thanks!

Before I saw your last post Matsom, however, I tried to re-organize my flags. I managed to get the code to not only compile correctly, but save the file and function how I want it to!


The only thing that now confuses me, is that I believe that on a program in the past, I did open the file stream in one set of brackets and close it in another, yet the code compiled. It just didn't actually save the file... Oh well!

I'll keep experimenting, but I managed to get my program to work and I'm happy!

Thanks Ciphermagi and Matsom for all your help!
Topic archived. No new replies allowed.