Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <chrono>
- //ofstream - output stream
- //ifstream - input stream
- //fstream - input/output stream
- // std::ios::app
- // std::ios::trunc
- // std::ios::out
- // std::ios::in
- // std::ios::binary
- int main()
- {
- srand(time(nullptr));
- std::fstream iofile;
- iofile.open("d:\\file.dat", std::ios::trunc | std::ios::out | std::ios::in | std::ios::binary);
- if ( !iofile)
- {
- std::cout << "IOERROR: can't create/open file!\n";
- return 1;
- }
- int val{};
- for (int i{ 0 }; i < 10; ++i)
- {
- //val = rand() % 10000;
- //for (int i{ 0 }; i < sizeof(int); ++i)
- //{
- // uint8_t dec{ ((uint8_t*)&val)[i] };
- // uint8_t hex{ (uint8_t)(dec % 16) };
- // char L{ hex < 10 ? '0' + hex : 'A' + (hex - 10) };
- // dec /= 16;
- // hex = dec % 16;
- // char H{ hex < 10 ? '0' + hex : 'A' + (hex - 10) };
- //
- // std::cout << H << L << ' ';
- //}
- //std::cout << '\n';
- iofile.write((char*)&i, sizeof(int));
- }
- iofile.seekg(0, std::ios::beg);
- while (!iofile.eof())
- {
- iofile.read((char*)&val, sizeof(int));
- if (iofile.fail())
- {
- break;
- }
- std::cout << val << '\n';
- iofile.seekg(1 * sizeof(int), std::ios::beg);
- }
- iofile.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement