user-0 提问: 解析字符串

回复:
user: user-1
content: sscanf也可以啊，sprintf也可以啊

user: user-1
content: 竟然不知道strtok

user: user-2
content: #include<stdio.h> #include<stdlib.h> #define MAX_P 4 char tmp[]="113421234123|435345345|675454545|78956456456"; int main(){ 	char *p[MAX_P]; 	char *t; 	int i=0,j; 	for(t=strtok(tmp,"|");t;t=strtok(NULL,"|")) p[i++]=t; 	for(j=0;j<i;++j) printf("p[%d]:%s\n",j,p[j]); 	return 0; }

user: user-3
content: str = tmp; p[0] = str; i = 1; while(*str) {          if(*str == '|') {                    p[i++] = ++str;                    continue;           }           ++str; }

user: user-4
content: strtok, wcstok, _mbstok Find the next token in a string.  char *strtok( char *strToken, const char *strDelimit );  wchar_t *wcstok( wchar_t *strToken, const wchar_t *strDelimit );  unsigned char *_mbstok( unsigned char*strToken, const unsigned char *strDelimit );  Routine Required Header Compatibility  strtok <string.h> ANSI, Win 95, Win NT  wcstok <string.h> or <wchar.h> ANSI, Win 95, Win NT  _mbstok <mbstring.h> Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  All of these functions return a pointer to the next token found in strToken. They return NULL when no more tokens are found. Each call modifies strToken by substituting a NULL character for each delimiter that is encountered.  Parameters  strToken  String containing token(s)  strDelimit  Set of delimiter characters  Remarks  The strtok function finds the next token in strToken. The set of characters in strDelimit specifies possible delimiters of the token to be found in strToken on the current call. wcstok and _mbstok are wide-character and multibyte-character versions of strtok. The arguments and return value of wcstok are wide-character strings; those of _mbstok are multibyte-character strings. These three functions behave identically otherwise.  Generic-Text Routine Mappings  TCHAR.H Routine  _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined  _tcstok strtok  _mbstok  wcstok     On the first call to strtok, the function skips leading delimiters and returns a pointer to the first token in strToken, terminating the token with a null character. More tokens can be broken out of the remainder of strToken by a series of calls to strtok. Each call to strtok modifies strToken by inserting a null character after the token returned by that call. To read the next token from strToken, call strtok with a NULL value for the strToken argument. The NULL strToken argument causes strtok to search for the next token in the modified strToken. The strDelimit argument can take any value from one call to the next so that the set of delimiters may vary.  Warning   Each of these functions uses a static variable for parsing the string into tokens. If multiple or simultaneous calls are made to the same function, a high potential for data corruption and inaccurate results exists. Therefore, do not attempt to call the same function simultaneously for different strings and be aware of calling one of these function from within a loop where another routine may be called that uses the same function.  However, calling this function simultaneously from multiple threads does not have undesirable effects.  Example  /* STRTOK.C: In this program, a loop uses strtok  * to print all the tokens (separated by commas  * or blanks) in the string named "string".  */  #include <string.h> #include <stdio.h>  char string[] = "A string\tof ,,tokens\nand some  more tokens"; char seps[]   = " ,\t\n"; char *token;  void main( void ) {    printf( "%s\n\nTokens:\n", string );    /* Establish string and get the first token: */    token = strtok( string, seps );    while( token != NULL )    {       /* While there are tokens in "string" */       printf( " %s\n", token );       /* Get next token: */       token = strtok( NULL, seps );    } }   Output  A string   of ,,tokens and some  more tokens  Tokens:  A  string  of  tokens  and  some  more  tokens   String Manipulation Routines  See Also   strcspn, strspn, setlocale

user: user-2
content: LZ需要strtok

user: user-5
content: 开头定义两个指针p1，p2，p2依次比较'|',相同则将p1-p2保存起来，然后将p2赋给p1，继续比较。

user: user-6
content: 引用 3 楼 zhao4zhong1 的回复:strtok, wcstok, _mbstok Find the next token in a string.  char *strtok( char *strToken, const char *strDelimit );  wchar_t *wcstok( wchar_t *strToken, const wchar_t *strDelimit );  unsigned char *_mbstok( unsigned char*strToken, const unsigned char *strDelimit );  Routine Required Header Compatibility  strtok <string.h> ANSI, Win 95, Win NT  wcstok <string.h> or <wchar.h> ANSI, Win 95, Win NT  _mbstok <mbstring.h> Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  All of these functions return a pointer to the next token found in strToken. They return NULL when no more tokens are found. Each call modifies strToken by substituting a NULL character for each delimiter that is encountered.  Parameters  strToken  String containing token(s)  strDelimit  Set of delimiter characters  Remarks  The strtok function finds the next token in strToken. The set of characters in strDelimit specifies possible delimiters of the token to be found in strToken on the current call. wcstok and _mbstok are wide-character and multibyte-character versions of strtok. The arguments and return value of wcstok are wide-character strings; those of _mbstok are multibyte-character strings. These three functions behave identically otherwise.  Generic-Text Routine Mappings  TCHAR.H Routine  _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined  _tcstok strtok  _mbstok  wcstok     On the first call to strtok, the function skips leading delimiters and returns a pointer to the first token in strToken, terminating the token with a null character. More tokens can be broken out of the remainder of strToken by a series of calls to strtok. Each call to strtok modifies strToken by inserting a null character after the token returned by that call. To read the next token from strToken, call strtok with a NULL value for the strToken argument. The NULL strToken argument causes strtok to search for the next token in the modified strToken. The strDelimit argument can take any value from one call to the next so that the set of delimiters may vary.  Warning   Each of these functions uses a static variable for parsing the string into tokens. If multiple or simultaneous calls are made to the same function, a high potential for data corruption and inaccurate results exists. Therefore, do not attempt to call the same function simultaneously for different strings and be aware of calling one of these function from within a loop where another routine may be called that uses the same function.  However, calling this function simultaneously from multiple threads does not have undesirable effects.  Example  /* STRTOK.C: In this program, a loop uses strtok  * to print all the tokens (separated by commas  * or blanks) in the string named "string".  */  #include <string.h> #include <stdio.h>  char string[] = "A string\tof ,,tokens\nand some  more tokens"; char seps[]   = " ,\t\n"; char *token;  void main( void ) {    printf( "%s\n\nTokens:\n", string );    /* Establish string and get the first token: */    token = strtok( string, seps );    while( token != NULL )    {       /* While there are tokens in "string" */       printf( " %s\n", token );       /* Get next token: */       token = strtok( NULL, seps );    } }   Output  A string   of ,,tokens and some  more tokens  Tokens:  A  string  of  tokens  and  some  more  tokens   String Manipulation Routines  See Also   strcspn, strspn, setlocalehttp://en.cppreference.com/w/c/string/byte/strtok

user: user-3
content: 引用 6 楼 a461874693 的回复:[quote=引用 4 楼 my_live_123 的回复:]  str = tmp; p[0] = str; i = 1; while(*str) {          if(*str == '|') { +                 *str = 0; //把 | 换掉                    p[i++] = ++str;                    continue;           }           ++str; }  [/quote] 修正一下

user: user-3
content: 引用 5 楼 xiadun 的回复:[quote=引用 4 楼 my_live_123 的回复:]  str = tmp; p[0] = str; i = 1; while(*str) {          if(*str == '|') {                    p[i++] = ++str;                    continue;           }           ++str; }   你这样输出不对 [/quote] 指向对应的地址，你的需求是什么？

user: user-7
content: char tmp[]="113421234123|435345345|675454545|78956456456"; 	char *p[4]; 	char* p1 = tmp, *p2 = NULL; 	int len = 0; 	for (int i = 0; i < 4;++i) 	{ 		p2 = p1 + len; 		p[i] = strtok(p2,"|"); 		len += strlen(p2) + 1 ; 	} 	for (int i = 0; i < 4; ++i) 	{ 		cout << p[i] << endl; 	} 	return 0;

user: user-7
content: 引用 4 楼 my_live_123 的回复: str = tmp; p[0] = str; i = 1; while(*str) {          if(*str == '|') {                    p[i++] = ++str;                    continue;           }           ++str; }  你这个不是他想要的结果吧

user: user-0
content: 引用 4 楼 my_live_123 的回复: str = tmp; p[0] = str; i = 1; while(*str) {          if(*str == '|') {                    p[i++] = ++str;                    continue;           }           ++str; }   你这样输出不对