これはうまくいきます:
/* You need the following includes and defines */
#include <stdio.h>
#include <iostream.h>
#include <string.h>
#define NULL_CHAR 0x0
int parse(char* data) {
const int LINE_SIZE=255; /* Should be long enough for your unparsed data */
const int MAX_FIELDS=99; /* Maximum number of fields */
char output[MAX_FIELDS][LINE_SIZE];
int i;
int output_field_count;
int output_char_idx;
for (i = 0; i < MAX_FIELDS; i++) {
strcpy(output[i], "");
}
output_field_count = 0;
output_char_idx = 0;
for (i = 0; i < LINE_SIZE; i++) {
if ((data[i] != ',') &&
(output_field_count < MAX_FIELDS) &&
((output_char_idx+1) < LINE_SIZE)) {
output[output_field_count][output_char_idx] = data[i];
output[output_field_count][output_char_idx+1] = NULL_CHAR;
output_char_idx++;
}
else if (data[i] == ',') {
output_field_count++;
output_char_idx = 0;
}
}
output_field_count++;
output_char_idx = 0;
printf("OUTPUT FIELD COUNT IS: %d\n", output_field_count);
for (i = 0; i < output_field_count; i++) {
printf("FIELD %i IS: %s\n", i, output[i]);
}
return 0;
}
これは次のように呼び出すことができます。
char data[500]; /* Should be long enough for your unparsed data */
strcpy(data, "atr#1,atr#2,...,atr#n");
parse(data);
strcpy(data, "btr#1,btr#2,...,btr#n");
parse(data);