Selecting any option (1-5) results in 'This file does not exist'. I've fiddled around, to no avail. I'm guessing it's something to do with the folder structure the files are actually in, but obviously, I'm not too sure.
The files themselves are in the Visual Studio project folder.
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
cout << "Willkommen zu meinem Programm!\n\n";
char choice;
do {
//User Prompt
cout << "Please Choose a Selection from the Menu.\n\n";
//Display Menu
cout << "1) Program Languages and their uses\n2) Different Programming Paradigms\n3) Key features of procedural programs\n4) The suitability of procedural programs for graphical applications\n5) Why modular elements, such as functions are a key element of programming \n6) Exit\n\n";
cin >> choice;
cout << endl;
if (choice == '1')
{
//Read the file
ifstream infile;
ofstream outfile;
//Open the File
infile.open("ProgrammingLanguages.txt", ios::in);
//Ensure File exists
if (!infile)
{
cout << "File does not exist!\n\n";
}
//Close the file
infile.close();
}
if (choice == '2')
{
//Read the file
ifstream infile;
ofstream outfile;
//Open the File
infile.open("ProgrammingParadigms.txt", ios::in);
//Ensure File exists
if (!infile)
{
cout << "File does not exist!\n\n";
}
//Close the file
infile.close();
}
if (choice == '3')
{
//Read the file
ifstream infile;
ofstream outfile;
//Open the File
infile.open("KeyFeatures.txt", ios::in);
//Ensure File exists
if (!infile)
{
cout << "File does not exist!\n\n";
}
//Close the file
infile.close();
}
if (choice == '4')
{
//Read the file
ifstream infile;
ofstream outfile;
//Open the File
infile.open("ProceduralSuitabilty.txt", ios::in);
//Ensure File exists
if (!infile)
{
cout << "File does not exist!\n\n";
}
//Close the file
infile.close();
}
if (choice == '5')
{
//Read the file
ifstream infile;
ofstream outfile;
//Open the File
infile.open("ModularElements.txt", ios::in);
//Ensure File exists
if (!infile)
{
cout << "File does not exist!\n\n";
}
//Close the file
infile.close();
}
elseif (choice == '6')
{
//Tell user to have nice day
cout << "A thousand blessings upon you and your family.\n";
cin.get();
cin.ignore();
}
} while (choice != '6');
return 0;
}
Have you tried to create an output file with a unique name?
An output stream has fewer problems because by default they will create the file if it doesn't exist. And if it creates the file you can use your operating systems find functionality to verify that you have your input files in the correct working directory, because this uniquely named file will be in the current working directory if it was created.
Changed what exactly? The point raised by oren drobitsky was a red herring, it was already ok.
What you need to do is to either
1. place the file in the correct directory where the program is looking for it.
or
2. specify the the full path to the file.
jlb above advised you on one way to find the correct directory (i.e. wherever the output file is created).
For the second approach, use the full path, such as "C:\\directoryname\\ProgrammingParadigms.txt" - modify to suit the actual location on your system.
The files are definitely in the correct place. Shouldn't I have a cout which actually displays the text contained within the text file? Otherwise, it's just skipping that and going straight to the fail.
I thought the problem was still as stated in the opening post,
Selecting any option (1-5) results in 'This file does not exist'
Has that issue now been resolved?
Otherwise, it's just skipping that and going straight to the fail.
If the file does exist, it goes straight to the close() with no message.
Shouldn't I have a cout which actually displays the text contained within the text file?
Yes, you should definitely be doing something with the infile. There's an example of reading and displaying a file using getline in a loop in the section on text files in the tutorial: http://www.cplusplus.com/doc/tutorial/files/