更改文本中的值并将其存储在 C 中的字符数组中
我需要一些有关以下场景的信息:我有一个存储有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 sprintf 就可以了。
Using
sprintf
will work.您应该使用
snprintf()
从格式化“template”,如下所示:在这里,我只模板化了第一个 IP 地址,并将其表示为四个
int
:s。您需要将其扩展到要动态格式化的其余字段。You should use
snprintf()
to build the string from a formatting "template", like so: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.