strtok 관련.. 질문입니다.

crescent0의 이미지

strtok로 ',' 를 구분해서 문자열을 짤라 내었습니다..

예를 들면 a, b, c, d, e 면
a
b
c
d
e
이렇게 값을 얻어오게 되게 햇는데;

a, b, c, d, e, , f
일 결우에 공백값은 얻어오지 못하더군요;

공백이 읽혀야 하는데;
공백을읽지 않고 바로 f를 가져 옮니다.;

어떻게 하면 공백이 읽힐까요;

불량청년의 이미지

a, b, c, d, e, , f

, , 사이에 공백문자가 포함되어 있나요?

공백문자가 없으니 당연히 f가 바로 나오겠죠.

H/W가 컴퓨터의 심장이라면 S/W는 컴퓨터의 영혼이다!

서지훈의 이미지

소스를 보여 주세요...
뭔가 잘 못 된 부분이 있을거 같은데...

#include <stdio.h>

int main(void)
{
    char test[80], blah[80];
    char *sep = "\\/:;=-\t";
    char *word, *phrase, *brkt, *brkb;

    strcpy(test, "This;is.a:test:of=the/str ing\\tokenizer- : : function.");

    printf("test = [%s]\n", test);

    for (word = strtok_r(test, sep, &brkt);
            word;
            word = strtok_r(NULL, sep, &brkt))
    {
        printf("word = [%s]\n", word);
    }
}

이건 잘 됨...

<어떠한 역경에도 굴하지 않는 '하양 지훈'>

#include <com.h> <C2H5OH.h> <woman.h>
do { if (com) hacking(); if (money) drinking(); if (women) loving(); } while (1);

niemand의 이미지

strtok 는 원래 그런거라고 알고 있습니다.
원하시는 기능을 하시려면
strsep 함수를 쓰세요.

추신..
공백값이라고 말씀하셔서 많은 분들이 혼란이 생깁니다.
공백값 또는 공백문자(space)와 진짜 아무 문자도 없는 것을 구분 안 해주셔서요 ^^;

/* strtok는 구분자 사이에 내용이 없으면 그냥 지나감 */
/* strsep는 구분자 사이에 내용이 없으면 빈 문자열 반환 */

#include <stdio.h>
#include <string.h>

char *strsep(register char **stringp, register const char *delim);

int main()
{
    char Buff[256];
    char *from;
    char *value;
    
    memset(Buff, 0x00, sizeof(Buff));
    memcpy(Buff, "a|b|c||e|", sizeof(Buff)-1);
    
    from = Buff;
    
    value = strsep(&from, "|");
    printf("%s\n", value);
    value = strsep(&from, "|");
    printf("%s\n", value);
    value = strsep(&from, "|");
    printf("%s\n", value);
    value = strsep(&from, "|");
    printf("%s\n", value);
    value = strsep(&from, "|");
    printf("%s\n", value);
    
}

char *strsep(register char **stringp, register const char *delim)
{
    register char *s;
    register const char *spanp;
    register int c, sc;
    char *tok;

    if ((s = *stringp) == NULL)
        return (NULL);
    for (tok = s;;) {
        c = *s++;
        spanp = delim;
        do {
            if ((sc = *spanp++) == c) {
                if (c == 0)
                    s = NULL;
                else
                    s[-1] = 0;
                *stringp = s;
                return (tok);
            }
        } while (sc != 0);
    }
    /* NOTREACHED */
}
Fe.head의 이미지

한가지 궁금한 사항이 있는데요..

C++ string class와 관련된 함수는 없나요?

고작 블로킹 하나, 고작 25점 중에 1점, 고작 부활동
"만약 그 순간이 온다면 그때가 네가 배구에 빠지는 순간이야"

ㅡ,.ㅡ;;의 이미지

strtok 는 규칙대로 아주 잘작동하는 함수입니다.
공백이 있다면 분명 공백을 얻어옵니다.


----------------------------------------------------------------------------

crescent0의 이미지

:oops:

공백이..아니라. 아무값도 없는 거였네요;

저도 이상하다고 계속 생각했었는데 ,, 제가 잘못한거 였네요

그러면 strsep써야겠네요..

감사합니다.~

doldori의 이미지

fehead wrote:
한가지 궁금한 사항이 있는데요..

C++ string class와 관련된 함수는 없나요?


string::find_first_of 가 대략 비슷한 일을 합니다.
이외에도 find_last_of, find_first_not_of, find_last_not_of 등의
멤버 함수가 제공됩니다.
yielding의 이미지

아래 링크에 C++로 구현한 split에 대한 내용이 있습니다
http://bbs.kldp.org/viewtopic.php?t=35996&highlight=yielding

Life rushes on, we are distracted

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.