1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
bool append(std::string filename, std::string content)
{
if (FILE* f = fopen(filename.c_str(), "ab"))
{
uint32_t len = hton(content.size());
fwrite(&len, sizeof(len), 1, f);
fwrite(content.c_str(), len, 1, f);
fclose(f);
return true;
}
return false;
}
bool append(std::string filename, const char* content)
{
append(filename, std::string(content));
}
std::vector<std::string> fetch(std::string filename)
{
std::vector<std::string> out;
if (FILE* f = fopen(filename.c_str(), "rb"))
{
// read each string and append to out
while (!feof(f))
{
uint32_t len;
fread(&len, sizeof(len), 1, f);
len = ntohl(len);
std::unique_ptr<char[]> buf(new char[len]);
fread(buf, len, 1, f);
out.emplace_back(buf, len);
}
fclose(f);
}
return out;
}
|