41#include <emscripten.h>
42#include <emscripten/val.h>
46 using emscripten::val;
47 EM_ASM(var fs = require(
'fs'); Module.fs = fs;);
48 val fs = val::module_property(
"fs");
49 string contents = fs.call<val>(
"readFileSync", string(filename),
50 string(
"binary")).as<string>();
56 using emscripten::val;
57 EM_ASM(var fs = require(
'fs'); Module.fs = fs;);
58 val fs = val::module_property(
"fs");
59 fs.call<val>(
"writeFileSync", string(filename),
60 emscripten::typed_memory_view(data.size(), (uint8_t *)data.data()),
80 FILE *f = fopen(filename.c_str(),
"rb");
83 fprintf(stderr,
"Could not open file %s: %s\n", filename.c_str(),
90 while ((nr = fread(buf, 1,
sizeof(buf), f)) > 0)
106 FILE *f = fopen(filename.c_str(),
"wb");
109 fprintf(stderr,
"Could not open file %s: %s\n", filename.c_str(),
116 while ((nr = fwrite(data.data() + offset, 1, data.size() - offset, f)) > 0)
119 if (offset >= data.size())
124 if (offset != data.size())
126 fprintf(stderr,
"error writing: %s, offset: %zu, size: %zu\n",
127 strerror(errno), offset, data.size());
string read_file_to_string(const string &filename)
Opens a file, reads the entire contents, stores it in a c++ std::string and returns this string.
void write_string_to_file(const string &filename, const string &data)
Opens (or creates) a file, truncates it and overwrites the contents with what is given in a string.