使用的sscanf解析字符串 [英] Using sscanf to parse a string
问题描述
使用GCC(海湾合作委员会)4.6.2在C89模式。
Using gcc (GCC) 4.6.2 in C89 mode.
我想知道如果我的的sscanf
已正确实施?
I am wondering if my sscanf
has been implemented correctly?
我想先手刚刚从它包含在这个元素串SDP的端口号,端口号为49462。
I am trying to just get just the port number from this SDP string which is contained in element, the port number is '49462'.
m=audio 49462 RTP/AVP 0 8
我使用的sscanf
是这样的:
sscanf(element, "%*s %d", &sdp.audio_port);
因此,将忽略第一个字符串的一部分'M =音频,然后得到的端口号,这正是我想要的。
So it will ignore the first string part 'm=audio' and then get the port number, which is what I want.
的端口号后面的字符串的最后部分可以被忽略。实际上可能会有更多的音频格式规定,即8 0 94 101,等于是字符串的长度可以验证。然而,这仅仅是音频端口我很感兴趣,而不是其他。
The final part of the string after the port number can be ignored. In fact there could be more audio formats specified i.e. 0 8 94 101, etc. So the string length could verify. However, it's just the audio port I am interested in and nothing else.
我会需要的字符串的其余部分指定的任何格式?
Would I need any format specified for the rest of the string?
我得到一些内存的问题,我想知道如果这可能是原因。
I was getting some memory issues, and am wondering if this could be the cause.
推荐答案
我跑codepad.org以下code和它工作得很好。
I ran the following code on Codepad.org, and it worked fine.
#include<stdio.h>
int main()
{
char *element= "m=audio 49462 RTP/AVP 0 8";
int num;
sscanf(element, "%*s %d", &num);
printf("%d\n",num);
return 0;
}
这也许像你描述的,有一个运行时间错误
别处美元有效code-片段p $ pventing预期的输出。
It maybe as you described, there was a Run Time Error
elsewhere preventing expected output for the valid code-snippet.
这篇关于使用的sscanf解析字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!