strtok, strsep¿Í °°Àº ÅäÅ« ÃßÃâ ÇÁ·Î±×·¥
ÃÑ ÆäÀÌÁö ¼ö : 3224

Àüü ÇÔ¼ö/¿ë¾î»çÀü
Facebook Joinc ±×·ì   Joinc QA »çÀÌÆ®
ÇöÀçÀ§Ä¡ : Code>C>token



joinc´Â Firefox¿Í chrome¿¡¼­ Å×½ºÆ® Çß½À´Ï´Ù. IE¿¡¼­´Â Å×À̺íÀÌ ±úÁö°Å³ª À̹ÌÁö°¡ º¸ÀÌÁö ¾ÊÀ» ¼ö ÀÖ½À´Ï´Ù. ƯÈ÷ ±¸±Û DocsÀ̹ÌÁöÀÇ °æ¿ì ¿¢¹Úó¸®µÉ ¼ö ÀÖ½À´Ï´Ù.

¼³¸í

»êÇÏ´ÔÀÇ ÅäÅ«ÃßÃâÇϱ⸦ Á¶°¢ÄÚµå ¸ðÀ½À¸·Î ¿Å°å½À´Ï´Ù.

strsepÀ̳ª strtok °°Àº ÅäÅ« ÃßÃâ ÇÔ¼ö¸¦ ¸¸µé¾îº»´Ù. ±âÁ¸ÀÇ Ç¥ÁØÇÔ¼öµéÀº ¸Þ¸ð¸®¸¦ ÇÒ´çÇÏ¸ç Æ÷ÀÎÅ͸¦ ¹ÝȯÇϴµ¥ À̰ÍÀÌ ¾ðÁ¦ ÇØÁ¦µÇ´ÂÁö ¾Ë ¼ö ¾ø´Ù. (¹°·Ð, glibc¼Ò½º¸¦ º¸¸éµÈ´Ù.) ¼Ò½º´Â º¸Áö ¾Ê¾ÒÁö¸¸ ´ë·«, »ç¿ëÀÚ°¡ Á÷Á¢ ÇØÁ¦ÇØ¾ß ÇÒ °Í °°¾Æ º¸ÀδÙ. ¶ÇÇÑ, µÑ´Ù manÀ» º¸¸é ÁÖÀǰ¡ º¸ÀδÙ. ±×·¡¼­, ¿ëµµ¿¡ ¸Â°í ´õ ÁÁÀº(³»°¡ º¸±â¿¡) ÇÔ¼ö¸¦ ¸¸µé¾î »ç¿ëÇÑ´Ù.

strsepÀÇ NOTES :

The strsep() function was introduced as a replacement for strtok(), since the latter cannot
handle empty fields. However, strtok() conforms to ANSI-C and hence is more portable.


strtokÀÇ BUGS :
Never use these functions. If you do, note that:
These functions modify their first argument.
These functions cannot be used on constant strings.
The identity of the delimiting character is lost.
The strtok() function uses a static buffer while parsing, so it's not thread safe.
Use strtok_r() if this matters to you.

»ç¿ë¹æ¹ý

char *token(char *str, char *src, const char* sep); 
 
  1. str : ÅäÅ« ºÐ¸®¸¦ À§ÇÑ ¿øº» ¹®ÀÚ¿­
  2. src : ÅäÅ« ºÐ¸®ÈÄ ´ÙÀ½ ¹®ÀÚ¿­À» °¡¸£Å°´Â Æ÷ÀÎÅÍ
  3. sep : ÅäÅ«
  4. return : ´õÀÌ»ó ÅäÅ«ÀÌ ¾ø°Å³ª '\0'À» ¸¸³ª¸é

#include "token.h" 
 
int main() { 
   char src[20]; 
   char str[20]; 
   char* next = &src[0]; 
   strcpy(src, "ab1de12eoqd12dq"); 
   while(next = token(str, next, "12")) { 
      printf("%s\n", str); 
   } 
   return(0); 
} 
 

ÄÚµå

#include <stdio.h> 
 
char* token(char* str, char* src, const char* sep) { 
   int i = 0; 
   if (*src == '\0') return(NULL); 
   while (1) { 
      if (sep[i] == '\0') { 
         str -= strlen(sep); 
         break; 
      } else if (*src == sep[i]) { 
         i++; 
      } else i = 0; 
      if (*src == '\0') break; 
      *str++ = *src++; 
   } 
   *str = '\0'; 
   return(src); 
} 
 

º¯°æ»çÇ×


2004/01/08

EmailÀ» ±âÀÔÇϸé, ´ñ±ÛÀÌ ¸ÞÀÏ·Î Àü´ÞµË´Ï´Ù.