Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2015-present The Bitcoin Core developers2// Copyright (c) 2017 The Zcash developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#include <util/readwritefile.h>8#include <util/fs.h>10#include <algorithm>11#include <cstdio>12#include <string>13#include <utility>15std::pair<bool,std::string> ReadBinaryFile(const fs::path &filename, size_t maxsize)16{17FILE *f = fsbridge::fopen(filename, "rb");18if (f == nullptr)19return std::make_pair(false,"");20std::string retval;21char buffer[128];22do {23const size_t n = fread(buffer, 1, std::min(sizeof(buffer), maxsize - retval.size()), f);24// Check for reading errors so we don't return any data if we couldn't25// read the entire file (or up to maxsize)26if (ferror(f)) {27fclose(f);28return std::make_pair(false,"");29}30retval.append(buffer, buffer+n);31} while (!feof(f) && retval.size() < maxsize);32fclose(f);33return std::make_pair(true,retval);34}36bool WriteBinaryFile(const fs::path &filename, const std::string &data)37{38FILE *f = fsbridge::fopen(filename, "wb");39if (f == nullptr)40return false;41if (fwrite(data.data(), 1, data.size(), f) != data.size()) {42fclose(f);43return false;44}45if (fclose(f) != 0) {46return false;47}48return true;49}