2023년 1월 13일 금요일

간단한 strtok_s 사용법

 원본 문자열을 ',' 로 분할하고 2차원 배열에 복사하는 소스이다.


#ifdef WIN32
#define strtok_r strtok_s    // 윈도우/리눅스 빌드
#endif

char m_szRecordPath[MAX_RECORD_PATH][MAX_PATH];
int m_nRecordPathCount;

void setRecordPaths(char *filepaths)
{
memset(&m_szRecordPath, 0, sizeof(m_szRecordPath));
m_nRecordPathCount = 0;
char *ret_ptr, *next_ptr;

ret_ptr = strtok_r(filepaths, ",", &next_ptr);
while (ret_ptr) {
memcpy(&m_szRecordPath[m_nRecordPathCount], ret_ptr, strlen(ret_ptr));
m_nRecordPathCount++;
ret_ptr = strtok_r(NULL, ",", &next_ptr);
}

for (int i=0; i<m_nRecordPathCount; i++)
printf("record path[%d] : %s\n", i, m_szRecordPath[i]);
}

댓글 없음:

댓글 쓰기