Haha

Kolorwanie:
Status:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>

void append_string(std::string file, std::string string) {
  // można std::ofstream<unsigned short> ale dużo jebania z dwoma streamami
  std::ofstream writeFileStream(file, std::ios::binary);
  // tu chyba castuje się na char
  writeFileStream << (unsigned short)string.size();

  std::copy(string.begin(), string.end(),
            std::ostreambuf_iterator<char>(writeFileStream));

  writeFileStream.close();
}

int read_strings(std::string file, std::string strings[]) {
  std::ifstream readFileStream(file, std::ios::binary);

  while (readFileStream.good()) {
    // tu wczytaj string length i string

    unsigned short stringLength;
    readFileStream.readsome();

    readFileStream.get();

    readFileStream.read(stringLength, stringLength.tellg());
  }

  readFileStream.close();
}

int main() {
  auto filePath = "string.bin";

  append_string(filePath, "foo");
  append_string(filePath, "bar");

  std::string readBuffer[2];

  read_strings(filePath, readBuffer);

  for (auto &string : readBuffer) {
    std::cout << string << std::endl;
  }
}

Copyrights 2014-2021 © Wklejaj.pl Wszelkie prawa zastrzeżone.