strtok와 strtok_r의 차이는?

maidland의 이미지

안녕하세요..

궁금한게 있어서 이렇게 글을 남깁니다..

strtok와 strtok_r의 차이는?

무엇인가요?

프로토타입이 다르다던가.. 먼가 다른게 있겠죠-ㅅ-;;

무엇이 다른가요?

cdpark의 이미지

man strtok

STRTOK(3)           Linux Programmer's Manual           STRTOK(3)

NAME
       strtok, strtok_r - extract tokens from strings

SYNOPSIS
       #include <string.h>

       char *strtok(char *s, const char *delim);

       char *strtok_r(char *s, const char *delim, char **ptrptr);

proto type도 당연히 다릅니다. :(

자세한 차이는 man page에 다 적혀 있습니다.

김충길의 이미지

thread safe 한 함수 입니다.

screen + vim + ctags 좋아요~

espereto의 이미지

strtok는 thread-unsafe,
strtok_r은 thread-safe 라는 특성이 있습니다.

멀티 쓰레드 프로그래밍을 할 때에는, thread-safe한 놈들로 골라서 해야하죠.

참고로, 표준 함수 이름에 _r이 붙은 함수들은 thread-safe 한 놈들입니다.
하는 일은 표준함수와 똑같죠.

maidland의 이미지

:o 답변 감사합니다..

char *t;
char name[25]="alert flags:M;bits:3";

t = strtok(name," ");
printf("%s\n",t);

이렇게 해주면 alert라고 출력됩니다..

위에 껏에 덧붙여..

t = strtok(NULL,";");
printf("%s\n",t);
	
t = strtok(t,":");
printf("%s\n", t);
	
t = strtok(NULL,":");
printf("%s\n",t);

이렇게 해주면...
flags:M
flags
M

요렇게 출력됩니다.. 위 소스를 합쳐서 출력시키면....

물론,

alert
flags:M
flags
M

이렇게 나오겠죠.. 제가 궁금한건 그 후 부터입니다...

flags:M의 뒷부분.. bits:3이부분도..

bits:3
bits
3

이런식으로 출력시키고 싶습니다.. 소스를 어떤식으로 추가시켜줘야할까요?

위와 유사하게 해도 잘되지않아서 이렇게 질문을 올립니다...

제가 바라는 최종 출력화면.........>ㅅ<

alert
flags:M
flags
M
bits:3
bits
3

이것입니다... 답변 부탁드립니다...

%p.s: while(p != NULL) 이것을 사용해서 해보았지만 잘안돼네요^^

>> http://ragnarok.co.kr <<

라그온+ㅁ+ 댄서는 아직 죽지 않았다!!
=-=-=-=-=-=-=-=-=-=-=-

dreampia의 이미지

int main(int argc, char **argv)
{
    char *t, *p;
    char name[25]="alert flags:M;bits:3";


    t = strtok(name, " ;\n");
    printf("%s\n", t);

    for ( ; ; )
    {
        if (! (t = strtok(NULL, " ;\n")))
            break;
        else
            printf("%s\n", t);

        if ((p = strchr(t, ':')))
        {
            printf("%.*s\n", strlen(t) - strlen(p), t);
            printf("%s\n", p+1);
        }
    }

    exit(0);
}


08:02:28 pxdev:temp] o( ")o $ make a
gcc -O2 -g -Wall -I. -c  a.c
gcc -O2 -g -Wall -I. -o a a.o 
[08:02:30 pxdev:temp] (o^^o) $ a
alert
flags:M
flags
M
bits:3
bits
3
[08:02:33 pxdev:temp] o( ")o $


>/dev/null 2>&1

maidland의 이미지

감사합니다^-^ :lol:

이걸 잘응용해 사용하겠습니다...

제가 사용하는게 한줄이 아니라 여러줄이고.. 옵션부의 길이도 더 길거든요^-^

잘 참고 하겠습니다..

감사합니다.. 좋은 하루 되십시요.. :P

>> http://ragnarok.co.kr <<

라그온+ㅁ+ 댄서는 아직 죽지 않았다!!
=-=-=-=-=-=-=-=-=-=-=-

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.