'File Does Not Exist' Issue

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.

Can anyone help?

Cheers.

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
#include <iostream>
#include <fstream>

using namespace 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();
		}

		else if (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;

}

Idea's anyone?
Last edited on
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.


closed account (1vD3vCM9)
Uhh I may be wrong but why do you do if (!infile)?
Shouldn't you use if(infile.fail()) { std::cout << "File Doesn't Exist!\n";}
Thanks, changed that, but still doesn't work. Probably something trivial that's missing.
Thanks, changed that, but still doesn't work.

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/
Last edited on
Thanks, everything is working as it should now.
Topic archived. No new replies allowed.