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.
#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>
usingnamespace 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...*/
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
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.