ACL_ARGV --- 字符串分割动态数组

  字符串分割是在程序编写过程中经常需要做的事情,如,将字符串:hello world, you are welcome!,进行单词分割,结果希望得到5个单词:hello, world, you, are, welcome。使用 ACL_ARGV 函数便可以非常轻松地实现此功能,如下:
  // 分割字符串,分割符为 ' ', '\t', ',', '!'
  ACL_ARGV *argv = acl_argv_split("hello world, you are welcome!", " \t,!");
  ACL_ITER iter;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在rabbitmq-c中传递JSON字符串,可以将其转换为字符数组,然后将其作为消息体发送到RabbitMQ消息队列中。以下是一个示例代码片段: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <stdint.h> #include <stdbool.h> #include <amqp.h> #include <amqp_framing.h> #include <amqp_tcp_socket.h> #include <jansson.h> int main(int argc, char const *const *argv) { const char *hostname; int port, status; char const *exchange; char const *routingkey; char const *messagebody; amqp_socket_t *socket = NULL; amqp_connection_state_t conn; // Set up the connection parameters hostname = "localhost"; port = 5672; exchange = "my-exchange"; routingkey = "my-routing-key"; // Create a connection object conn = amqp_new_connection(); // Create a TCP socket object socket = amqp_tcp_socket_new(conn); if (!socket) { printf("Error creating TCP socket object\n"); return 1; } // Connect to the RabbitMQ server status = amqp_socket_open(socket, hostname, port); if (status) { printf("Error connecting to RabbitMQ server\n"); return 1; } // Authenticate with the RabbitMQ server amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"); amqp_channel_open(conn, 1); amqp_get_rpc_reply(conn); // Create a JSON object json_t *root; root = json_pack("{s:s, s:s}", "name", "John", "age", "30"); if (!root) { printf("Error creating JSON object\n"); return 1; } // Convert the JSON object to a string messagebody = json_dumps(root, JSON_COMPACT); if (!messagebody) { printf("Error converting JSON object to string\n"); return 1; } // Create a message object amqp_basic_properties_t props; props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG; props.content_type = amqp_cstring_bytes("text/json"); props.delivery_mode = 2; // persistent delivery mode amqp_bytes_t message_bytes = amqp_cstring_bytes(messagebody); amqp_basic_publish(conn, 1, // channel number amqp_cstring_bytes(exchange), amqp_cstring_bytes(routingkey), 0, // mandatory 0, // immediate &props, message_bytes); amqp_get_rpc_reply(conn); // Clean up json_decref(root); free(messagebody); amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS); amqp_connection_close(conn, AMQP_REPLY_SUCCESS); amqp_destroy_connection(conn); return 0; } ``` 在上面的代码中,我们使用了JSON-C库来创建和转换JSON对象。我们首先创建一个JSON对象,然后将其转换为字符串,最后将字符串作为消息体发布到RabbitMQ消息队列中。注意,我们还需要设置消息属性,例如内容类型和持久性标志。消息的发送和接收可以使用RabbitMQ-C库中提供的一些函数进行操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值