user-0 提问: 求教  sscanf_s读入的字符串中有"|"导致崩溃

回复:
user: user-1
content: 你把参数搞错了。  CString cs = "11 山山|水水"; //11后有空格 int a=654321; char  str[100] = ""; sscanf_s(str, "%s%d", cs ,a );

user: user-0
content: 结贴 算了不传多个字符参数了

user: user-0
content: 感谢3楼的回复,虽然没懂我的意思  int a,b; CString cs; char  str[100] = ""; cs = "456 38  abc"; //123 456 后都有空格 sscanf_s(cs, "%d%d%s", &a,&b, str);  //到这里都是对的  cs = "456 38  a|bc"; //abc字符串中有符号 | sscanf_s(cs, "%d%d%s", &a,&b, str); //到这里就崩溃了  cs = "|";  sscanf_s(cs, "%s", str); //这样还崩溃 //看来就是符号的问题了  我要做的从cs的字串中 分离出包含符号 "|" 的字符串,  这个字符串是不定长的,可能像例子中,也可能只"aa|bb|cc" 或更多用符号 "|" 间隔的字符串  请问如何解决?

user: user-2
content: sscanf_s不是楼主这么用的！ Run-Time Library Reference  sscanf_s, _sscanf_s_l, swscanf_s, _swscanf_s_l  Example  See Also  Send Feedback     Read formatted data from a string. These are versions of sscanf, _sscanf_l, swscanf, _swscanf_l with security enhancements as described in Security Enhancements in the CRT.    int sscanf_s(    const char *buffer,    const char *format [,       argument ] ... ); int _sscanf_s_l(    const char *buffer,    const char *format,    locale_t locale [,       argument ] ... ); int swscanf_s(    const wchar_t *buffer,    const wchar_t *format [,       argument ] ... ); int _swscanf_s_l(    const wchar_t *buffer,    const wchar_t *format,    locale_t locale [,       argument ] ... );    Parameters buffer Stored data  format Format-control string. For more information, see Format Specifications.  argument Optional arguments  locale The locale to use  Return Value Each of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end of the string is reached before the first conversion.  If buffer or format is a NULL pointer, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, these functions return -1 and set errno to EINVAL  For information on these and other error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.  Remarks The sscanf_s function reads data from buffer into the location given by each argument. The arguments after the format string specify pointers to variables with a type that corresponds to a type specifier in format. Unlike the less secure version sscanf, a buffer size parameter countOfBuffer is required when using the type field characters c, C, s, S and [. This parameter must be supplied as an additional parameter after each buffer which requires it. For more information, see scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l and scanf Type Field Characters.  Note:  The size parameter is of type unsigned, not size_t.    The format argument controls the interpretation of the input fields and has the same form and function as the format argument for the scanf_s function. If copying takes place between strings that overlap, the behavior is undefined.  swscanf_s is a wide-character version of sscanf_s; the arguments to swscanf_s are wide-character strings. sscanf_s does not handle multibyte hexadecimal characters. swscanf_s does not handle Unicode full width hexadecimal or "compatibility zone" characters. Otherwise, swscanf_s and sscanf_s behave identically.  The versions of these functions with the _l suffix are identical except that they use the locale parameter passed in instead of the current thread locale.  TCHAR.H routine   _UNICODE & _MBCS not defined  _MBCS defined  _UNICODE defined   _stscanf_s  sscanf_s  sscanf_s  swscanf_s   _stscanf_s_l  _sscanf_s_l  _sscanf_s_l  _swscanf_s_l    Requirements Routine  Required header   sscanf_s, _sscanf_s_l  <stdio.h>   swscanf_s, _swscanf_s_l  <stdio.h> or <wchar.h>    For additional compatibility information, see Compatibility in the Introduction.  Example   Copy Code  // crt_sscanf_s.c // This program uses sscanf_s to read data items // from a string named tokenstring, then displays them.  #include <stdio.h> #include <stdlib.h>  int main( void ) {    char  tokenstring[] = "15 12 14...";    char  s[81];    char  c;    int   i;    float fp;     // Input various data from tokenstring:    // max 80 character string plus NULL terminator    sscanf_s( tokenstring, "%s", s, _countof(s) );    sscanf_s( tokenstring, "%c", &c, sizeof(char) );    sscanf_s( tokenstring, "%d", &i );    sscanf_s( tokenstring, "%f", &fp );     // Output the data read    printf_s( "String    = %s\n", s );    printf_s( "Character = %c\n", c );    printf_s( "Integer:  = %d\n", i );    printf_s( "Real:     = %f\n", fp ); }     Copy Code  String    = 15 Character = 1 Integer:  = 15 Real:     = 15.000000    .NET Framework Equivalent See Parse methods, such as System::Double::Parse.  See Also Concepts Stream I/O fscanf, _fscanf_l, fwscanf, _fwscanf_l scanf, _scanf_l, wscanf, _wscanf_l sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l _snprintf, _snprintf_l, _snwprintf, _snwprintf_l Send feedback on this topic to Microsoft.

user: user-0
content: 谢谢2楼的答复 前面我打错了,重新描述下 CString cs = "123 山山|水水|青青|草草"; //123后有空格 int a; char  str[100] = ""; sscanf_s(cs, "%d%s", a, str); 我要做的从cs的字串中 分离中数值"123""到a中,  剩下的"山山|水水|青青|草草" 要存到str中 这个子串是不定长的,可能像例子中,也可能只"山山|水水" 或更多用符号 | 分割的字串 上面的代码运行会崩溃  后来发现 CString cs = "|";  char  str[100] = ""; sscanf_s(cs, "%s", str); 只要字串中有"|" 就会崩溃 请问如何解决?