2012-07-24 5 views
-1

다음을 분리하려고합니다. strtok 함수로 나누어야하고 값 1.2597을 얻고 싶습니다. Down은 변경 될 수있는 동적 단어입니다. 이 경우에는 구분 기호로 공백을 사용할 수 있고 통화 인 [1] 값을 얻을 수 있지만 어떻게 처리해야하는지 이해합니다. 당신은 또한 double 수 당신이 float에 수를 변환 할 경우C++이 경우에 strtok을 사용하는 방법

char *first = strtok(string, ' '); 
char *second = strtok(0, ' '); 

또는 :

아래로 0.0021 (0.16 %) 14시 32분 SGT [44]

+0

? 당신은 당신이해야 할 말을 .... – Nahum

+0

strtok 기존 문자열을 수정합니다. 그걸 사용 하시겠습니까? – ThirdOne

+2

[무엇을 시도 했습니까?] (http://mattgemmell.com/2008/12/08/what-have-you-tried/) –

답변

1

CCY 1.2597이 그것을해야

char tmp[5]; 
float number; 
sscanf(string, "%s %f", tmp, &number); 

을 또는 당신이 strtok에있어 숫자 토큰에 sscanf를 사용 sscanf 사용합니다.

1

쉽게 Boost.Regex로를 사용하고 안전하게이 작업을 얻을 수 있습니다

// use a regular expression to extract the value 
std::string str("CCY 1.2597 Down 0.0021(0.16%) 14:32 SGT [44]"); 
boost::regex exp("CCY (\\d+\\.\\d+)"); 
boost::match_results<std::string::const_iterator> match; 
boost::regex_search(str, match, exp); 
std::string match_str(res[1].first, res[1].second) 

// convert the match string to a float 
float f = boost::lexical_cast<float>(match_str); 
std::cout << f << std::endl; 
0

일부인 모든 문자로 구분 된 연속 된 문자 시퀀스입니다 토큰에이 기능 분할의 str을 호출의 순서 구분자의.

예 :

char str[] = "now # is the time for all # good men to come to the # aid of their country"; 
char delims[] = "#"; 
char *result = NULL; 
result = strtok(str, delims); 
while(result != NULL) { 
    printf("result is \"%s\"\n", result); 
    result = strtok(NULL, delims); 
} 

출력 : 질문을 IUS 무엇

result is "now " 
result is " is the time for all " 
result is " good men to come to the " 
result is " aid of their country" 
관련 문제