
`ini_parse_quantity` 함수는 INI 파일에서 Quantity 값을 추출하는 데 사용되는 함수입니다.
이 함수를 사용하기 위해선 INI 파일을 읽어들이기 위한 준비작업이 필요합니다. 일반적으로 `ini_parse` 함수를 사용하여 INI 파일을 파싱한 후, `ini_parse_quantity` 함수를 사용하여 Quantity 값을 추출할 수 있습니다.
예를 들어, INI 파일에 "quantity = 100" 이라고 적혀있을 때, `ini_parse_quantity` 함수를 사용하여 Quantity 값을 추출하는 방법은 다음과 같습니다.
#hostingforum.kr
c
#include
#include
#include
// INI 파일을 파싱하는 함수
void ini_parse(const char *filename, char **sections, int *num_sections) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Error opening file '%s'n", filename);
return;
}
char line[1024];
char section[1024];
char key[1024];
char value[1024];
*num_sections = 0;
while (fgets(line, sizeof(line), file)) {
// Section 이름이면 section 변수에 저장
if (line[0] == '[' && line[strlen(line) - 1] == ']') {
strcpy(section, line + 1);
section[strlen(section) - 1] = ' ';
sections[*num_sections] = strdup(section);
(*num_sections)++;
}
// Key-Value 쌍이면 key와 value 변수에 저장
else if (line[0] != ';' && line[0] != 'n' && line[0] != ' ') {
char *equal_pos = strchr(line, '=');
if (equal_pos != NULL) {
strcpy(key, line);
key[equal_pos - line] = ' ';
strcpy(value, equal_pos + 1);
value[strcspn(value, "n")] = ' ';
// Quantity 값을 추출하는 코드를 여기에 추가할 수 있습니다.
}
}
}
fclose(file);
}
// Quantity 값을 추출하는 함수
int ini_parse_quantity(const char *filename, char *section, char *key) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Error opening file '%s'n", filename);
return -1;
}
char line[1024];
int quantity = -1;
while (fgets(line, sizeof(line), file)) {
// Section 이름이면 section 변수와 일치하는지 확인
if (line[0] == '[' && line[strlen(line) - 1] == ']') {
char *section_name = line + 1;
section_name[strlen(section_name) - 1] = ' ';
if (strcmp(section_name, section) == 0) {
// Key-Value 쌍이면 key와 value 변수에 저장
char *equal_pos = strchr(line, '=');
if (equal_pos != NULL) {
char *key_name = line;
key_name[equal_pos - line] = ' ';
if (strcmp(key_name, key) == 0) {
char *value = equal_pos + 1;
value[strcspn(value, "n")] = ' ';
quantity = atoi(value);
break;
}
}
}
}
}
fclose(file);
return quantity;
}
int main() {
char *sections[10];
int num_sections = 0;
ini_parse("example.ini", sections, &num_sections);
char section[] = "Section1";
char key[] = "quantity";
int quantity = ini_parse_quantity("example.ini", section, key);
if (quantity != -1) {
printf("Quantity: %dn", quantity);
} else {
printf("Quantity not foundn");
}
return 0;
}
위의 예제에서는 `ini_parse` 함수를 사용하여 INI 파일을 파싱한 후, `ini_parse_quantity` 함수를 사용하여 Quantity 값을 추출합니다. `ini_parse_quantity` 함수는 Section 이름과 Key 이름을 매개변수로 받으며, Quantity 값을 반환합니다. 만약 Section 이름이나 Key 이름이 일치하지 않으면, -1을 반환합니다.
2025-07-24 18:25