Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- int main() {
- streampos begin, end;
- // streampos type
- /*
- streampos is a specific type used for buffer and file positioning.
- It is the type returned by file.tellg(). Values of this type can
- be safely subtracted from other values of the same type, and can
- also be converted to an integer type large enough to contain the
- size of the file.
- These stream positioning functions can use two particular types:
- streampos and streamoff. These types are also defined as member
- types of the stream class.
- */
- ifstream myfile ("binary_file.bin", ios::binary);
- begin = myfile.tellg(); // get the start position of the stream
- myfile.seekg (0, ios::end);
- // modifies the get postion to be ...
- // 0 offset from the end of the stream
- end = myfile.tellg(); // get the end position of the stream
- if (end-begin == 0) {
- ofstream outFile ("binary_file.bin", ios::binary);
- char words[256] = {};
- cout << "Enter a series of words into"
- << " the file (enter: 'CTR+C' to terminate): \n";
- while (cin.get(words, sizeof(words))) {
- cout << "Writing [" << sizeof(words) << " bytes] to file...\n";
- outFile.write(words, sizeof(words));
- }
- outFile.close();
- } else {
- myfile.close();
- cout << "size is: " << (end-begin) << " bytes.\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement