C++ file operation snippets
4 stars based on
File streams include two member functions specifically designed to input and output binary data sequentially: Their prototypes of these functions are as follows:. It provides the address of an array of bytes where the read data elements are stored or from where the data elements to be written are taken.
The following program takes user input from std:: File streams have internal stream pointers. Pointer to read the data from the file is named the get pointer. Pointer to write the data from the file is named the put pointer. There are four member functions to manipulate the stream pointers:. The following functions deal with basic operations on files. The following program implements file copy using standard C library functions. A bitwise operation operates on one or two bit patterns represented by binary numbers at the level of their individual bits:.
A bitwise NOT or complement, is a unary operation which performs logical negation on each bit, forming the ones' ifstream read int from binary file of the given binary value. Digits which were 0 become 1, and vice versa. A bitwise OR takes two bit patterns of equal length, and produces another one of the same length by matching up corresponding bits the first of each; the second of each; and so on and performing the logical OR operation on each pair of corresponding bits.
In each pair, the result is 1 if the first bit is 1 OR the second bit is 1 or bothand otherwise the result is ifstream read int from binary file. A bitwise AND takes two binary representations of equal length and performs the logical AND operation on each pair of corresponding bits.
In each pair, the result is 1 if the first bit is 1 AND the second bit is 1. Otherwise, the result is 0. A bitwise exclusive OR takes two bit patterns of equal length and performs the logical XOR operation on each pair of corresponding bits. The result in each position is 1 if the two bits are different, and 0 if they are the same. Take a look at the following program.
It encrypts the input ifstream read int from binary file prints result on the screen. To decrypt the output, merely reapplying the key removes the cipher. In cryptography, a simple XOR cipher is a relatively simple encryption algorithm that operates according to the principles:.
The XOR operator is extremely common as a component in more complex ciphers. Its primary merit is that it is simple to implement, and that the XOR operation is computationally inexpensive. By itself, using a constant repeating ifstream read int from binary file, a simple XOR cipher can trivially be broken. However, if the key is as long as the message so it is never repeated and its bits are random, it is in effect a "one-time pad", which is unbreakable in theory.
The following program uses encryption to encrypt a file. An interesting aspect of this utility is that it is using the same file stream to both read and write individual bytes into a file:.
Their prototypes of these functions are as follows: There are four member functions to manipulate the stream pointers: Get current read position in the stream: Get current write position ifstream read int from binary file the stream ofstream:: Opens the named file, and returns a stream, or NULL if the attempt fails. Legal values for mode include: Writes one character, converted to an unsigend char into a file.
Function returns the character written, or EOF for error. Returns the next character of stream as an unsigend charconverted to an intor EOF if ifstream read int from binary file of file or error occurs. Flushes any unwritten data for stream, discards any unread buffered input, frees any automatically allocated buffer, then closes the stream. It returns EOF if any errors occurred, and zero otherwise. A bitwise operation operates on one or two bit patterns represented by binary numbers at the level of their individual bits: This program tends to produce non-printable output.
An interesting aspect of this utility is that it is using the same file stream to both read and write individual bytes into a file: