我写这段代码是为了试试leveldb。我使用 Unix 时间作为键。对于有空格的值,只保存最后一部分。这是代码。我正在运行 Linux 内核 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;
最佳答案
问题不在 leveldb 中,而是在您读取输入的方式中:
std::string note;
std::cin >> note;
这将只读到第一个空格。这是常见的错误,例如:
关于c++ - 为什么这个 leveldb 代码会截断其中有空格的 "std::string"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40990918/