我在使用Redis编写的C程序(使用hiredis C绑定)。Redis:当数据以换行符开始时的SET命令
这里是我的代码:
void insert(redisContext* c,char* buf){
static redisReply *reply;
const char* hash="asdf";
char* cmd=(char*)malloc((strlen("SET ")+strlen(hash)+strlen(" ")+CHUNKSIZE)*sizeof(char));
//hash=getHash(buf);
memcpy(cmd,"SET ",(size_t)strlen("SET "));
memcpy(cmd+strlen("SET "),hash,(size_t)strlen(hash));
memcpy(cmd+strlen("SET ")+strlen(hash)," ",(size_t)strlen(" "));
memcpy(cmd+strlen("SET ")+strlen(hash)+strlen(" "),buf,(size_t)CHUNKSIZE);
fwrite(cmd,strlen("SET ")+strlen(hash)+strlen(" ")+CHUNKSIZE,sizeof(char),stdout);
printf("\n\n\n\n\n\n");
reply=(redisReply*)redisCommand(c,cmd);
freeReplyObject(reply);
free(cmd);
}
正如你所看到的,CMD的样子:SET asdf xxx
,其中xxx为512个字节长(二进制数据)。
当二进制数据字符串以'\ n'开头时会出现问题。我不断收到错误(分段错误)。
任何人有任何想法?
非常感谢,
非常感谢您的反馈意见。这显然是一些其他的错误。非常感激, – Eamorr 2011-12-19 14:30:00