1

I wrote this piece of code to try leveldb. I am using Unix time as keys. For values that have spaces, only the last part gets saved. Here is the code. I am running Linux Kernel 4.4.0-47-generic

  while (true) {
    std::string note;
    std::string key;
    std::cout << "Test text here ";
    std::cin >> note;
    std::cout << std::endl;

    if(note.size() == 0 || tolower(note.back()) == 'n' ) break;
    key = std::to_string(std::time(nullptr));
    status = db->Put(write_options, key, note);

    if(!status.ok()) break;
  }

  std::cout << "Read texts........" << std::endl;
  leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions());
  for(it->SeekToFirst(); it->Valid(); it->Next()){
      std::cout << it->key().ToString() << "  " << it->value().ToString() << std::endl;
  }

  delete db;
Peter Chaula
  • 3,456
  • 2
  • 28
  • 32

1 Answers1

1

The issue is not in leveldb, but in the way you read the input:

std::string note;
std::cin >> note;

This will read only up to the first whitespace. It is common mistake, see for example:

reading a line from ifstream into a string variable

michalsrb
  • 4,703
  • 1
  • 18
  • 35