Failure to open file path specified by user

Peers,

What can I do to correct this problem:

This program is supposed to open a file specified by the user. My problem is that I can't get it to open the file. I can make it open a program I specify within the source code, that is simple. I have been poring over forums and such for hours, and have probably made several dozen changes to this code, to no avail. I am starting to think it might just be the way I'm entering the file name.

When I run the program, and it prompts me to enter the file name, this is how I enter it: I:\Programming\Encryption_and_Decryption\infile.txt

The underscores aren't part of the actual file path, but it counts three separate inputs if I use spaces. This may also have something to do with it.

Here is what I'm working with:

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

using namespace std;

void codex(const string buffer, const char map[], int length, string& buffer2)
{
	char letter;

	for (int i = 0; i < buffer.length(); i++)
	{
		if (isupper(buffer[i]))
		{
			letter = buffer[i] - 'A';
			letter += map[i % length];
			letter = letter + 'A';
			buffer2.push_back(letter);
		}

		else if (islower(buffer[i]))
		{
			letter = buffer[i] - 'a';
			letter += map[i % length];
			letter = letter + 'a';
			buffer2.push_back(letter);
		}

		else
		{
			buffer2.push_back(buffer[i]);
		}
	}
}


void encrypt(string encryptKey, char map[])
{
	for ( int i = 0; i < encryptKey.length(); i++) 
		
		map[i] = encryptKey[i] - 'a';
}

void decrypt(string decryptKey, char map[])
{
	for (int i = 0; i < decryptKey.length(); i++)

		map[i] = decryptKey[i] + 'a';
}

void main()
{
	string end[] = {"encrypt", "decrypt"},
		   ekey,
		   buffer,
		   buffer2;
	char file[100],
	     enmap[128],
		 demap[128],
		 repeat = 'y';
	int maplength,
		answer;
	ifstream infile;
	ofstream outfile;

	do
	{
		cout << "Would you like to encrypt or decrypt? (1 or 2) ";
		cin >> answer;

		while (answer != 1 && answer != 2)
		{
			cout << "\n\n1 or 2: ";
			cin >> answer;
		}

		cout << "\n\nEnter the name of the file you wish to " << end[answer - 1] << ".";
		cout << "\n\n";
		cin >> file;
		infile.open(file);

		while (!infile.is_open())
		{
			cout << "\n\nThe file could not be opened successfully. Please try again.";
			cin >> file;
			infile.open(file);
		}

		cout << "\n\nEnter the name of your output file: ";
		cout << endl;
		cin >> file;
		outfile.open(file);

		cout << "\n\nEnter your encrypion key (128 characters max): " << "\n\n";
		cin >> ekey;

		while (ekey.length() > 128)
		{
			cout << "\n\nYour key is too long, enter another one: " << "\n\n";
			cin >> ekey;
		}

		maplength = ekey.length();

		if (answer == 1)
		{
			encrypt(ekey, enmap);
		}

		else 
		{
			decrypt(ekey, demap);
		}

		while (infile)
		{
			getline(infile, buffer);

			if (answer == 1)
			{
				codex(buffer, enmap, maplength, buffer2);
			}
			else
			{
				codex(buffer, demap, maplength, buffer2);
			}
		}

		outfile.close();
		outfile.clear();
		infile.close();
		infile.clear();
		buffer2.erase(0);

		cout << "\n\nWould you like to run the application again? (y or n): ";
		cin >> repeat;
	}
	while (repeat == 'y' || repeat == 'Y');
}


Any help at all would be greatly appreciated, thank you for your time.
Last edited on
Hi

Try to debug your code( run it in debug mode, put some break points)
see and watch your codes what they are really doing, and what they are really expected to do.
do it step by step, you learn more and see your own bugs and errors

hope it helps
Last edited on
I'm sorry, but no. I've tried to put in breakpoints and single step it but I don't really understand the ostream window that pops up as a result. I'm not exactly an expert in c++. My limited understanding of it tells me that

cin >> file; infile.open(file);

should assign whatever I enter as "file" and then open that file, should it exist. It's not that the file doesn't exist, or the spelling of it for that matter, as I've been very meticulous about that sort of thing.
Last edited on
Hi

You have forget to save the data into output file , you may add some thing like

outfile<<buffer2<<endl; before line 132, before you close the output file, outfile.close();

hope it helps
Last edited on
Topic archived. No new replies allowed.