int main() { ifstream ifs("input.pdf", ios_base::binary); if(!ifs) { cerr << "Could not read input file as binary" << endl; return 0; } vector<int> v; // read input bytes int x; while(ifs.read(as_bytes(x), sizeof(x))) { v.push_back(x); } ofstream ofs("output.pdf", ios_base::binary); if(!ofs) { cerr << "Could not create output binary file" << endl; return 0; } for(int i: v) { ofs.write(as_bytes(i), sizeof(i)); } }
int main(int argc, char ** argv) { string iname; cout << "Enter file name:"; cin >> iname; ifstream ifs{iname}; if (!ifs) error("Unable to open file ", iname); string oname; cout << "Enter file name for saving:"; cin >> oname; ofstream ofs{ oname, ios_base::binary }; if (!ofs) error("Unable to open file " + oname + " for writing."); string s; int win_eol = 0x0a0d; while (ifs && getline(ifs, s)) { ofs.write(s.c_str(), s.size()) ; ofs.write(as_bytes(win_eol), 2); } return 0; }
// converts text file s to binary void to_binary(const string& s) { ifstream ifs(s.c_str()); if (!ifs) error("can't open input file ",s); ofstream ofs(bin_file.c_str(),ios_base::binary); if (!ofs) error("can't open output file ",bin_file); char ch; while (ifs.get(ch)) { ofs.write(as_bytes(ch),sizeof(char)); } }
// converts the binary file to text file s void from_binary(const string& s) { ofstream ofs(s.c_str()); if (!ofs) error("can't open output file ",s); ifstream ifs(bin_file.c_str(),ios_base::binary); if (!ifs) error("can't open input file ",bin_file); char ch; while (ifs.read(as_bytes(ch),sizeof(char))) { ofs << ch; } }
bool FlashDevice::comparePage(const void* data, flash_addr_t address, page_size_t length) { uint8_t buf[STACK_BUFFER_SIZE]; page_size_t offset = 0; while (offset<length) { page_size_t toRead = min(sizeof(buf), length-offset); if (!readPage(buf, address+offset, toRead)) break; if (!memcmp(buf, as_bytes(data)+offset, toRead)) break; offset += toRead; } return offset==length; }
constexpr uint64_t fnv_1a_64 (const Data &data, uint64_t h = 0xcbf29ce484222325ULL) noexcept { return fnv_1a_64(as_bytes(span(data)), h); }