为什么这个leveldb代码会截断" std :: string" s中有空格?

时间:2016-12-06 08:29:40

标签: c++ c++11 leveldb

我写了这段代码来试试leveldb。我使用Unix时间作为键。对于具有空格的值,仅保存最后一部分。这是代码。我正在运行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;

1 个答案:

答案 0 :(得分:1)

问题不在于leveldb,而在于您阅读输入的方式:

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

这将只读取第一个空格。这是常见的错误,例如:

reading a line from ifstream into a string variable