간단한 문자열 파싱을 하는데 있어서 문의드립니다.

segenny의 이미지

매번 질문만 드리네요..^^;

다음과 같은 문자열을
A"1000";B"2000";

char string[]="A\"1000\";B\"2000\";";
의 값을 A=1000, B=2000 으로 대입할 수 있게 파싱할 수 있는 방법이 없을까요?

일일이 자르면 되겠지만 일반 파싱함수로 하려니 힘드네요..
전문가님들의 조언 부탁드립니다..

감사합니다.

익명사용자의 이미지

char string[]="A\"1000\";B\"2000\";";
int a, b;
char ca, cb;
sscanf(string, "%c\"%d\";%c\"%d\";", &ca, &a, &cb, &b);

poplinux의 이미지

훔.. 명쾌한 답변입니다.

========================
조직 : E.L.D(Embedded Linux Developer/Designer)
블로그 : poplinux@tistory.com
카페 : cafe.naver.com/poplinux

임베디드 리눅스 관련 프리렌서 지향

익명사용자의 이미지

더더더 공부만이 .. 살길이네요..^^;

kalstein의 이미지

주어진건 간단한 문자열이니까...저렇게 해도 되지만, 좀 더 복잡해지면..

boost의 token 시스템이나...(토크나이저였나? 이름은 가물가물..;;)

정규식이나...좀 많이 복잡한 거라면 spirit도 있습니다 ^^;;

뭐...yacc를 쓰는 방법도 있지요 ㅡ _-;;


------------------------------------------
Let`s Smart Move!!
http://kalstein.tistory.com/

오호라의 이미지

#include <string.h>
#include <stdio.h>
 
#define MAX_DATA	10
 
typedef struct _tag_sturct
{
	char var;
	long value;
} node;
 
int main( int argc, char* argv[] )
{
	char string[] = "A\"1000\";B\"2000\";";
	char seps[] = "\";\\";
	char *token;
	node foobar[MAX_DATA];
	int i = 0;
	int count = 0;
 
	memset( foobar, 0, sizeof(node) * MAX_foobar );
 
	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" */
		foobar[count].var = *token;
		/* Get next token: */
		token = strtok( NULL, seps );
 
		foobar[count].value = atoi( token );
		/* Get next token: */
		token = strtok( NULL, seps );
 
		count++;
	}
 
	for( i = 0 ; i < count ; i++ )
	{
		printf( " %c=%d\n", foobar[i].var, foobar[i].value );
	}
 
	return 0;
 
}

segenny님께서 원하시는 소스는 이런게 아닐까요...

1. MSDN strtok() 소스 약간 수정
2. thread-safety 하지 않죠. strtok()가...

※ 단순한건 strxxx()계열로 처리하시고, syntax로 넘어가면 lex계열로...

Hello World.

segenny21의 이미지

성의없는 질문도 너무 성의께 답변 주셔서 감사합니다..

^^;

댓글 달기

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