Writing vectors to ofstream
May 7, 2016 at 9:55pm UTC
So I'm working on a program to encrypt a file by reading a file, adding 8 to the ACSII code, and writing it to a second file. I've never done casting before so this could be completely wrong. It runs without errors but doesn't write anything to the Encrypt.txt and doesn't confirm the password. It just asks for the password and pauses the system. Please help :(
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
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
using namespace std;
//Encrypt by adding 8 to each value of vector
void encryption(vector<int > out, ifstream& encrypt, ofstream& decrypt)
{
int ACSII, x;
char text1,text2;
int i = 0;
vector<char > in;
//Read file and put into vector
while (!encrypt.eof())
{
encrypt >> text1;
//Convert char to int
ACSII = (int )text1;
//Add 8
x = ACSII + 8;
//Convert back
text2 = (char )x;
out.push_back(text2);
}
//Write encrypted file
copy(out.begin(), out.end(), ostream_iterator<float >(decrypt));
}
//Print decrypted contents
void print(ifstream encrypt)
{
string line;
while (!encrypt.eof())
{
getline(encrypt, line);
cout << line << endl;
}
}
int main()
{
ifstream password, encrypt;
ofstream decrypt;
string pass, entry;
vector<int > out;
//Open File.txt
encrypt.open("G:\\College\\2016 Spring\\C++\\Final\\File.txt" );
if (!encrypt)
{
cerr << "Unable to open file. \n" ;
system("pause" );
return 1;
}
//Open Encrypt.txt
decrypt.open("G:\\College\\2016 Spring\\C++\\Final\\Encrypt.txt" );
if (!decrypt)
{
cerr << "Unable to open file. \n" ;
system("pause" );
return 1;
}
//Call encryption function
void encryption(vector<int > out, ifstream& encrypt, ofstream& decrypt);
//Open password file
password.open("G:\\College\\2016 Spring\\C++\\Final\\Password.txt" );
if (!password)
{
cerr << "Unable to find password. \n" ;
system("pause" );
return 1;
}
//Get password into char data type
getline(password, pass);
//Ask for password
cout << "Please enter password: " ;
cin >> entry;
//Confirm password
if (entry == pass)
{
//Call print function if password is correct
void print(ifstream encrypt);
}
if (entry != pass)
{
cout << "Incorrect." ;
}
password.close();
decrypt.close();
encrypt.close();
system("pause" );
return 0;
}
May 7, 2016 at 10:20pm UTC
1 2
//Call encryption function
void encryption(vector<int > out, ifstream& encrypt, ofstream& decrypt);
Not a function call
Here's an example of a function call
getline(password, pass);
note how the parameters are written.
Topic archived. No new replies allowed.