sscanf 질문

qiiiiiiiip의 이미지


input_string = "{1,20},{3,30},{5,60},{12,142}";

와 같은 형식의 스트링을 파싱해서,

Point p[];

p[0].x = 1, p[0].y = 20
....
p[3].x = 12, p[3].y = 143

와 같이 세팅하려고 합니다.

전체 point의 개수가 가변이라고 했을때, 이 작업을 가장 간단하게 할 수 있는 방법이 뭘까요?

가급적이면 sscanf를 써서 할 수 있을까요?

raymundo의 이미지

음... 가장 간단하게 할 수 있는 건,

1. 정규표현식을 쓸 수 있는 스크립트 언어를 골라잡아 공부한다.
2. 그 언어와 C 코드의 바인딩 방법을 공부한다.
3. 이제 input_string의 값을 그 언어쪽으로 넘긴 후 그 언어에서 정규식을 써서 p 배열에 넣고 다시 C 코드로 되돌리도록 짠다.

...

죄,죄송합니다ㅠㅠ 갑자기 농담의 의욕이 막 솟아나서...

strtok 를 쓰거나
sscanf 와 index 를 써서 할 수 있겠습니다. "{%d,%d}" 포맷으로 읽고, 다음 번 읽을 자리는 index를 써서 '{'의 위치를 찾으면 되겠네요.

정규식을 쓰는 것도 좋겠지만, 제가 딱 한번 C의 regcomp, regexec 를 써서 정규식을 써보려다가 스크립트 언어에서 정규식 쓸 때와는 차원이 다르게 힘들어서 홧병나는 줄 알았던 기억이 있어서...

좋은 하루 되세요!

raymundo의 이미지

아래처럼 해봤는데, 여기선 그냥 Point의 배열을 일단 넉넉히 잡았지만, 그게 아까우면 포인터의 배열로 바꾸고 malloc으로 할당받아가며 만들어도 되겠지요.

    Point p[100];
    int idx = 0;
    char *input_string = "{1,20},{3,30},{5,60},{12,142}";
    char *ptr = input_string;
 
    while ( ptr != NULL ) {
        sscanf(ptr, "{%d,%d}", &p[idx].x, &p[idx].y);
        ptr++;
        ptr = index(ptr, '{');
        idx++;
    }

좋은 하루 되세요!

qiiiiiiiip의 이미지


저도 '{' 의 인덱스를 쓰는 방법으로 일단 해결했는데,

index() 라는 함수가 있었네요.. 새로운것을 알게 되었네요..

감사합니다~

댓글 달기

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