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!
//Preprocessor Directives
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
usingnamespace 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);
}
elseif (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;
}
//Preprocessor Directives
#include <iostream>
#include <iomanip.h>
#include <string.h>
#include <stdio.h>
#include <fstream>
#include <vector>
usingnamespace 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);
}
elseif (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;
}
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.
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.
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.
//Preprocessor Directives
#include <iostream>
#include <iomanip.h>
#include <string.h>
#include <stdio.h>
#include <fstream>
#include <vector>
usingnamespace 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.}
//Preprocessor Directives
#include <iostream>
#include <iomanip.h>
#include <string.h>
#include <stdio.h>
#include <fstream>
#include <vector>
usingnamespace 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.
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!