지난번에 boost::tokenizer를 소개했었습니다.
이 tokenzier는 약간 복잡한 형태의 토크나이징도 할 수 있다는 장점이 있습니다.
특히 엑셀이나 액세스등의 DB를 다룰 때 많이 사용되는 .CSV (Comma-Seperated Values) 파일을 토크나이즈하는 기능을 구현하는 것은 쉽지 않습니다.
보통 .CSV 파일은 "" 안에 공백이나 ,(comma)등이 포함될 수 있기 때문입니다.
다음은 escaped_list_separator를 사용하여 .CSV의 한 라인에 해당하는 문자열을 토크나이즈하는 예제입니다.
#include <iostream>
#pragma hdrstop
#include <boost/tokenizer.hpp>
#include <string>
#include <iterator>
int main(){
using namespace std;
using namespace boost;
string s = "Field 1,\"putting quotes around fields, allows commas\",Field 3";
tokenizer<escaped_list_separator<char> > tok(s);
// 루프를 사용하는 방법
for (tokenizer<escaped_list_separator<char> >::iterator i = tok.begin();
i != tok.end(); ++i)
cout << *i << endl;
// copy 알고리듬을 사용하는 방법
copy(tok.begin(), tok.end(), ostream_iterator<string>(cout, "\n"));
return 0;
}
|