简体   繁体   English

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

[英]Change the values in a text and store it a char array in C

I need some information regarding a scenario where I have an array stored with SIP INVITE message 我需要一些有关使用SIP INVITE消息存储数组的方案的信息

char array_invite[] =        "INVITE sip:302@1.2.3.4 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:302@1.2.3.4>; \r\n"
                             "From: \042Client\042<sip:305@5.6.7.8>;\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. 我想更改IP地址(1.2.3.4和5.6.7.8)和ID号(302和305)的硬代码值,并使其动态,以便我想在终端输出中手动输入值,以便每个会话我都可以连接到不同的远程地址。 Since I am not that fluent in CI am posting this question. 由于我不太了解CI,因此发布了此问题。

Anybody has an idea of how this can be done in C, may be an example would be good 任何人都知道如何在C中完成此操作,可能是一个很好的例子

Regards Dev 关于开发

Using sprintf will work. 使用sprintf将起作用。

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

You should use snprintf() to build the string from a formatting "template", like so: 您应该使用snprintf()从格式“模板”构建字符串,如下所示:

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:302@1.2.3.4>; \r\n"
                             "From: \042Client\042<sip:305@5.6.7.8>;\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. 在这里,我仅模板化第一个IP地址,并将其表示为四个int :s。 You will need to extend this for the rest of the fields that you want to format dynamically. 您将需要将此扩展到要动态格式化的其余字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM