更改文本中的值并将其存储在 C 中的字符数组中

发布于 2024-11-02 02:42:16 字数 1215 浏览 6 评论 0原文

我需要一些有关以下场景的信息:我有一个存储有 SIP INVITE 消息的数组

char array_invite[] =        "INVITE sip:[email protected] SIP/2.0\r\n"
                             "Via:SIP/2.0/UDP 5.6.7.8:39708;\r\n"
                             "Max-Forwards: 70\r\n"
                             "Contact:<sip:305@ 5.6.7.8>\r\n"
                             "To: <sip:[email protected]>; \r\n"
                             "From: \042Client\042<sip:[email protected]>;\r\n"
                             "Call-ID: abcdefg\r\n"
                             "CSeq: 1 INVITE\r\n"
                             "Content-Type: application/sdp\r\n"
                             "Content-Length: 142\r\n";

,我想更改 IP 地址(1.2.3.4 和 5.6.7.8)和 ID 号(302 和 305)的硬编码值并使其生效动态,这样我想在终端输出中手动输入值,以便对于每个会话我可以连接到不同的远程地址。由于我对 CI 不太熟悉,所以发布了这个问题。

任何人都知道如何在 C 中完成此操作,可能是一个很好的

例子 开发者

I need some information regarding a scenario where I have an array stored with SIP INVITE message

char array_invite[] =        "INVITE sip:[email protected] SIP/2.0\r\n"
                             "Via:SIP/2.0/UDP 5.6.7.8:39708;\r\n"
                             "Max-Forwards: 70\r\n"
                             "Contact:<sip:305@ 5.6.7.8>\r\n"
                             "To: <sip:[email protected]>; \r\n"
                             "From: \042Client\042<sip:[email protected]>;\r\n"
                             "Call-ID: abcdefg\r\n"
                             "CSeq: 1 INVITE\r\n"
                             "Content-Type: application/sdp\r\n"
                             "Content-Length: 142\r\n";

I want to change the hard code values for the IP Address (1.2.3.4 and 5.6.7.8) and the ID number(302 and 305) and make it dynamic such that I want to enter the values manually in my terminal output so that for each session I can connect to different remote addresses. Since I am not that fluent in C I am posting this question.

Anybody has an idea of how this can be done in C, may be an example would be good

Regards
Dev

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尴尬癌患者 2024-11-09 02:42:16

使用 sprintf 就可以了。

char array_invite[MAXLENGTH];
sprintf(array_invite,"Meet me at port %d\n",portnum);

Using sprintf will work.

char array_invite[MAXLENGTH];
sprintf(array_invite,"Meet me at port %d\n",portnum);
薄荷港 2024-11-09 02:42:16

您应该使用 snprintf() 从格式化“template”,如下所示:

char buffer[4096];
int ip{[4];

ip[0] = 1;
ip[1] = 2;
ip[2] = 3;
ip[3] = 4;

snprintf(buffer, sizeof buffer, "INVITE sip:302@%d.%d.%d.%d SIP/2.0\r\n"
                             "Via:SIP/2.0/UDP 5.6.7.8:39708;\r\n"
                             "Max-Forwards: 70\r\n"
                             "Contact:<sip:305@ 5.6.7.8>\r\n"
                             "To: <sip:[email protected]>; \r\n"
                             "From: \042Client\042<sip:[email protected]>;\r\n"
                             "Call-ID: abcdefg\r\n"
                             "CSeq: 1 INVITE\r\n"
                             "Content-Type: application/sdp\r\n"
                             "Content-Length: 142\r\n", ip[0], ip[1], ip[2], ip[3]);

在这里,我只模板化了第一个 IP 地址,并将其表示为四个 int:s。您需要将其扩展到要动态格式化的其余字段。

You should use snprintf() to build the string from a formatting "template", like so:

char buffer[4096];
int ip{[4];

ip[0] = 1;
ip[1] = 2;
ip[2] = 3;
ip[3] = 4;

snprintf(buffer, sizeof buffer, "INVITE sip:302@%d.%d.%d.%d SIP/2.0\r\n"
                             "Via:SIP/2.0/UDP 5.6.7.8:39708;\r\n"
                             "Max-Forwards: 70\r\n"
                             "Contact:<sip:305@ 5.6.7.8>\r\n"
                             "To: <sip:[email protected]>; \r\n"
                             "From: \042Client\042<sip:[email protected]>;\r\n"
                             "Call-ID: abcdefg\r\n"
                             "CSeq: 1 INVITE\r\n"
                             "Content-Type: application/sdp\r\n"
                             "Content-Length: 142\r\n", ip[0], ip[1], ip[2], ip[3]);

Here, I only templatized the first IP address, and represented it as four int:s. You will need to extend this for the rest of the fields that you want to format dynamically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文