0

In my C application as shown below I am trying to add a string as the member in a redis sorted set. Looks like it is not working as expected

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <hiredis/hiredis.h>

int main() {
    redisContext *redis = redisConnect("10.10.26.139", 7010);
    if (redis == NULL || redis->err) {
        if (redis) {
            printf("Redis connection error: %s\n", redis->errstr);
            redisFree(redis);
        } else {
            printf("Failed to allocate Redis context\n");
        }
        return 1;
    }

    // Construct the Redis command
    char command[1024];
    snprintf(command, sizeof(command), "ZADD myset 20233142239 \"2238 22 38 00 328 453 328 453 6 18 6 148 42 57 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 328 0 0 0\"");

    // Execute the command
    redisReply *reply = redisCommand(redis, command);
    if (reply == NULL) {
        printf("Redis command error: %s\n", redis->errstr);
        redisFree(redis);
        return 1;
    }

    // Check the reply
    if (reply->type == REDIS_REPLY_INTEGER) {
        printf("ZADD returned %lld\n", reply->integer);
    } else {
        printf("Redis command error: %s\n", reply->str);
    }

    // Free the reply and disconnect from Redis
    freeReplyObject(reply);
    redisFree(redis);

    return 0;
}

When I execute the above program instead of adding 1 entry i end up seeing 7 junk entries as shown in below picture

enter image description here

But if I execute the same command from REDISCLI it works great

ZADD myset 20233142239 "2238 22 38 00 328 453 328 453 6 18 6 148 42 57 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 328 0 0 0"

enter image description here

How do i send the string properly ? Do i need to use string packing ?

codingfreak
  • 4,467
  • 11
  • 48
  • 60

0 Answers0