Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <boost/filesystem/v3/operations.hpp>
- #include <boost/interprocess/file_mapping.hpp>
- #include <boost/interprocess/mapped_region.hpp>
- #include <boost/array.hpp>
- #include <fstream>
- #include <ctime>
- struct random_generator {
- random_generator() { std::srand((unsigned)std::time(NULL)); }
- char operator ()() {
- int res = std::rand() % (26 + 26 + 10);
- switch (res / 26) {
- case 0:
- return res + 'A';
- case 1:
- return res - 26 + 'a';
- case 2:
- return res - 26 * 2 + '0';
- default:
- return 0;
- }
- }
- };
- int main()
- {
- using namespace std;
- using namespace boost;
- const size_t filesize = (16 + 2) * 20000000;
- {
- // 生成一个指定大小的文件
- ofstream fout("mapped_file.txt");
- filesystem3::resize_file("mapped_file.txt", filesize);
- }
- {
- interprocess::file_mapping mfile("mapped_file.txt", interprocess::read_write);
- interprocess::mapped_region region(mfile, interprocess::read_write);
- array<char, 18> * addr = (array<char, 18> *)region.get_address();
- array<char, 18> * const end = addr + 20000000;
- random_generator rg;
- for (; addr != end; ++addr) {
- generate_n(addr->begin(), 16, rg);
- (*addr)[16] = '\r';
- (*addr)[17] = '\n';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement