My first program i decided to make an encryptor using character casting into an int and then using a rolling code to change the value and then cast back into the new charater.
I gradually improved this so it can now read a file using getline() and re-write the modified version, however this only works for single lines in the file.
Because i want it to re-write to file it'll encrypt the first line but delete the rest of the file only leaving the modified first line.
Is there an equivalant to the getline() that get the whole file for me INCLUDING white-spaces and newlines...
Or would somebody be able to post a way to iterate through the lines saving each to a 2dimentional vector or something...
Sounds like you are reading a line and then encrypting it then writing that line back to file? If this is the case, instead, read the entire file, line by line, use a vector and push_back each line of the file until you have read it's contents. Then encrypt each element of the vector (which contain 1 line of your file each). Then write the entire vector to the file, line by line.
#include <fstream>
#include <string>
#include <iterator>
int main()
{
// for encryption, open in binary mode
std::ifstream input_file( "input_file_name", std::ios::binary ) ;
// read the entire contents of the file into a string
std::string contents( std::istreambuf_iterator<char>(input_file),
( std::istreambuf_iterator<char>() ) ) ;
// encrypt the contents
// ...
// and write it out
std::ofstream output_file( "output_file_name", std::ios::binary ) ;
output_file << contents ;
}
Thank you to codekiddy for that read() function and the link, this looks like it could work quite work and is also nice and simple.
clanmjc who mentioned using vectors...
i'd already tried using this along with an iterator that can select what line to read but unfortunately i couldn't figure out how to detect when this is on the last line/if it needs to read the next line.
And thanks o anyone else who's contributed but i think for now i'm going to go for the read() function mentioned before along with the file size detection using the seekg() and tellg() mainly because i already know a little about these functions and how to use them, i just didn't think of this method.