我使用下面的函数来循环通过一对开放CDB哈希表。有时用一个额外的字符返回沿着一个给定键的值(具体地为CTRL-P(DLE字符/ 0x16 / 0o020))。

我已经检查了CDB键/值对与几个不同的公用事业和其中没有显示出附加到值的任何附加的字符。

我得到的字符,如果我使用cdb_read()或cdb_getdata()(下面的注释的代码)。

如果我猜我会说我做错了什么与缓冲区创建得到来自国家开发银行的功能的结果。

任何建议或协助是极大的赞赏。

char* HashReducer::getValueFromDb(const string &id, vector <struct cdb *> &myHashFiles)
{

  unsigned char hex_value[BUFSIZ];
  size_t hex_len;

  //construct a real hex (not ascii-hex) value to use for database lookups
  atoh(id,hex_value,&hex_len);

  char *value = NULL;
  vector <struct cdb *>::iterator my_iter = myHashFiles.begin();
  vector <struct cdb *>::iterator my_end = myHashFiles.end();


  try
  {
    //while there are more databases to search and we have not found a match
    for(; my_iter != my_end && !value ; my_iter++)
    {
      //cerr << "\n looking for this MD5:" << id << " hex(" << hex_value << ") \n";
      if (cdb_find(*my_iter, hex_value, hex_len)){
          //cerr << "\n\nI found the key " << id << " and it is " << cdb_datalen(*my_iter) << " long\n\n";
          value = (char *)malloc(cdb_datalen(*my_iter));
          cdb_read(*my_iter,value,cdb_datalen(*my_iter),cdb_datapos(*my_iter));
          //value = (char *)cdb_getdata(*my_iter);
          //cerr << "\n\nThe value is:" << value << " len is:" << strlen(value)<< "\n\n";
        };

    }
  }
  catch (...){}
  return value;
}
有帮助吗?

解决方案

首先,我不熟悉国开行,我不相信你有你的软件环境,足够的细节在这里。

不过,假设它是像其他数据库库我用...

的值可能没有被NUL终止的。这意味着,铸造char *与打印也不会工作。你应该将自己添加一个0字节。

所以malloc的cdb_datalen + 1,并设置的最后一个字符为0,然后打印出来。

更好的是,使用calloc它将分配内存已经设置为零。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top