Code Example—Encrypting a File
The following program reads a file, encrypts it, and writes the output to another file.
It includes three files:
: The C++ startup program reads the names of the input and output files from the command line.
It calls from a loop that reads blocks of data from a file, encrypts it, and writes the translated buffer to a new file.
: The header file contains a single function prototype:
void Encrypt( char* buf, long count, char eChar );
: The procedure loops through a character array and each character with a predefined value.
To run this program from a command prompt, pass the names of the input and output files, for example,
Encrypt infile.txt outfile.txt
Some of the following C++ commands are used in the program:
bool eof( ) const;:
The function returns true if the End-Of-File has been reached.
streamsize gcount( ) const;:
Returns the number of bytes extracted by the last unformatted input operation performed on this object.
ostream& write( const char* str , streamsize n );:
Inserts into the output stream a sequence of characters starting by character pointed by str and following by successive characters in the array until the number of characters specified by n has been successfully written.
“I’m not arguing, I’m just explaining why I’m right.”
― Unknown