Encryption/decrytption program. Files not opening.

Hello everyone, this is my first post to this website. I have a problem with my code that I cant seem to track down. I dont know if my files arent being passed to the functions correctly or what, but any help would be greatly appreciated! Code output is at the end.

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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
 

#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>

using namespace std;

void encrypt(fstream&,fstream&);
void decrypt(fstream&,fstream&);
void display(fstream&);

int main()

{
	fstream inFile;
	fstream outFile;
	string infileName;
	string outfileName;


	cout << "\n\t  *Make sure to include the extention (eg .txt)* ";
	cout << "\nPlease enter the name of the file you wish to work with.";
	cin >> infileName;
	cout << "\nPlease enter the name of the file you wish to copy your data to.";
	cin >> outfileName;

	outFile.open(outfileName,ios::in|ios::out);
	inFile.open(infileName, ios::in|ios::out);
	cout<<endl << endl;

	if (inFile.fail()||outFile.fail())
	{
		cout << "\n\t ERROR.";
	}
	else 
	{
		while (inFile)
		{
			cout << "\nHere is the file in its original form: " << endl<<endl;
			display(inFile);

			cout << "\nHere is the encrypted file: " << endl<<endl;
			encrypt(inFile, outFile);
			display(outFile);


			cout << "\nHere is the decrypted file: " << endl<<endl;
			decrypt(inFile, outFile);
			display(inFile);
		}
	}

		inFile.close();
		outFile.close();
	
	cout << "\nType any key to EXIT...";
	_getch();

	return 0;

}//end main()


/***********************************************************/
/*This function displays the contents of a certain file,*/
/*depending on which one is passed to it                 */
/********************************************************/
void display(fstream& FILE)
{

	string line;

	while (getline(FILE, line))
	{
		
		cout << line << endl;
	}
}



/**********************************************************/
/*This is a simple encryption function that accepts both */
/*the input and output files                            */
/*******************************************************/
void encrypt(fstream& inFILE, fstream& outFILE)
{
	char ch;

	if (inFILE)
	{
		inFILE.get(ch);
		while (inFILE)
		{
			outFILE.put((int)(ch));
			inFILE.get(ch);
		}

	}
	else
		cout << "\n\tFile could not be opened.";
}


/*********************************************************************/
/*The decryption function is similar to the encryption function     */
/*It accepts both input and output files and reverses what was done*/
/*during the encrytion process                                    */
/*****************************************************************/
void decrypt(fstream& inFILE, fstream& outFILE)
{
	char ch;

	if (outFILE)
	{
		    outFILE.get(ch);
		while (outFILE) 
		{
			inFILE.put((char)(ch));
			outFILE.get(ch);
			
		}
	
	}
	else
		cout << "\n\tFile could not be opened.";
}

	




/*
OUTPUT

Make sure to include the extension (eg .txt).
Please enter the name of the file you wish to work with: vifile.txt

Please enter the name of the file you wish to copy your data to: output.txt



Here is the file in its original form:

Hello there. This is a file. It has stuff
in it. Very important stuff.
Go away.

Here is the encrypted file:

	File could not be opened.

Here is the decrypted file:

	File could not be opened.

Type any key to EXIT...*/
Last edited on
Try
if (inFile.is_open() && (outFile.is_open()))
It gave me the ERROR from line 35.. so I'm guessing one of them didn't open. Most likely the outFile, but I don't know why. Is it possible that fstream wont work with the outFile? Perhaps I need to change it to ofstream, but then I wont be able to open it for input. I must be making this problem a lot harder than it actually is. I do that alot :/

EDIT: Now I just get the same output I had before. "File could not be opened" out of both functions
Last edited on
What I suggest is to start simple, and add any other features gradually, testing at each stage.
1
2
3
4
5
6
7
8
9
10
11
    string infileName;
    string outfileName;

    cout << "\n\t  *Make sure to include the extention (eg .txt)* ";
    cout << "\nPlease enter the name of the file you wish to work with.";
    cin >> infileName;
    cout << "\nPlease enter the name of the file you wish to copy your data to.";
    cin >> outfileName;

    ifstream inFile(infileName);
    ofstream outFile(outfileName);


Note - you never need to output anything to the input file - to do so could corrupt or destroy the data. So use an ifstream for the input.

I'd also start out with an ofstream for the output - you don't need to read anything from the file - in fact it might not even exist - at the start of the program.

Get the simple case of just encrypting the input and writing it to the output working first, before considering the other requirements.

The loop while (inFile) at line 39 I don't think is needed, I can't see what purpose it serves.
Last edited on
Topic archived. No new replies allowed.