How to get rid of extra data in character buffers for key and data
609342Nov 19 2007 — edited Nov 20 2007Hi -
Using the C API, I am using a cursor to just pull all the key/value pairs from a database.
Here is the relevant code:
/* Initialize key/data structures. */
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
key.flags = DB_DBT_MALLOC;
data.flags = DB_DBT_MALLOC;
if((ret = dbp->cursor(dbp, NULL, &cursor, 0)) != 0) {
dbp->err(dbp, ret, "%s", DATABASE);
goto err;
}
while((ret = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) {
fprintf(cgiOut, "%s|||%s\n", (char *)key.data, (char *)data.data);
free(key.data);
free(data.data);
}
if(ret != DB_NOTFOUND) {
dbp->err(dbp, ret, "cursor->c_get");
goto err;
}
This produces lines that in many cases contain extra characters at the ends, as though the malloced data was not being cleared before use. I wrote this routine originally in Java, and solved the problem there with the "setReuseBuffer(false)" Database Entry routine. Is there something similar in C?
Garey Mills