Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正确的sscanf使用问题_C_String_Gcc_Printf_Scanf - Fatal编程技术网

正确的sscanf使用问题

正确的sscanf使用问题,c,string,gcc,printf,scanf,C,String,Gcc,Printf,Scanf,我有一个字符串存储在一个名为解密的数组中,如下所示: char decrypted[5000]; sprintf(decrypted,"%s","192.168.0.2<==>6060<==>this is ipsec data"); char ip_source[20]; int port_source; char message[1000]; sscanf(decrypted,"%s<==>%d<==>%s",ip_source,&

我有一个字符串存储在一个名为
解密的数组中,如下所示:

char decrypted[5000];
sprintf(decrypted,"%s","192.168.0.2<==>6060<==>this is ipsec data");
char ip_source[20];
int port_source;
char message[1000];

 sscanf(decrypted,"%s<==>%d<==>%s",ip_source,&port_source,message);
 printf("source = %s\nport = %d\nmessage = %s\n",ip_source,port_source,message);
如何使消息作为完整字符串读取,而不在第一个空格处停止

编辑2:

source = 192.168.0.2<==>6060<==>this
port = 0
message =
source = 192.168.0.2
port = 6060
message = this

我能不能再纵容一下你们这些好人

下面是我试图读取的另一个字符串:

sprintf(decrypted,"%s","S=192.168.0.2|D=192.168.0.4||<==>S=6060");
sscanf(decrypted,"S=%s|D=%s||<==>S=%d",&ip_source,&ip_dest,&port_source);
sprintf(已解密,“%s”,“s=192.168.0.2 | D=192.168.0.4 | s=6060”);
sscanf(已解密,“S=%S | D=%S | S=%D”、&ip|源、&ip|目的地和端口|源);
它再次引发了问题。我尝试实施您的解决方案,尝试如下解决:

char decrypted[5000];
sprintf(decrypted,"%s","192.168.0.2<==>6060<==>this is ipsec data");
char ip_source[20];
int port_source;
char message[1000];

 sscanf(decrypted,"%s<==>%d<==>%s",ip_source,&port_source,message);
 printf("source = %s\nport = %d\nmessage = %s\n",ip_source,port_source,message);

sscanf(已解密,“S=%[^问题是第一个
%S
不知道在哪里停止。替换为
%[^
编辑:

source = 192.168.0.2<==>6060<==>this
port = 0
message =
source = 192.168.0.2
port = 6060
message = this
同样的规则也适用于将要存储在
消息
中的内容。假设
消息
只能包含集
a-z
a-z
和空格中的字符,您可能希望尝试以下操作:

sscanf(decrypted,"%[^<]<==>%d<==>%[A-Za-z ]",ip_source,&port_source,message);
另外,
&ip\u source
&ip\u dest
将是错误的,因为它们被定义为字符数组-您不需要引用运算符

最后,考虑向缓冲区添加限制检查,如其他答案所述:

sscanf(decrypted,"%19[^<]<==>%d<==>%999[a-z]",ip_source,&port_source,message);

sscanf(decrypted,“%19[^我喜欢解密的大小是5000,但扫描的比特数只有1000多一点。现在是该死的2013年。我们能不能停止以任何程度的接受度来处理缓冲区溢出问题?”我能不能再纵容你们这些好人一点这不是聊天室,这是一个问答网站。这是一个问题。也许你不喜欢我在提问前的礼貌。随你的便。无论如何,我感谢你的帮助
%[%@GrijeshChauhan是的,这是一个打字错误。现在已修复。@sukhvir查看编辑,它修复了输入字符串尾部分隔符的问题。[^#]的+1。很好,我没有发现消息结束太早的问题。这仍然会写到
消息
的末尾,所以我会厌倦调用它“完全修好了”。非常感谢。谢谢you@sukhvir既然您接受了我的答案,我也建议您使用dasblinkenlight建议的缓冲区限制检查。这很重要,所以我用这些信息更新了我的答案。谢谢,我也实施了限制检查
sscanf(decrypted,"%19[^<]<==>%d<==>%999[a-z]",ip_source,&port_source,message);