개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.07.24 18:24

INI 파일에서 Quantity 추출하는 방법에 대한 질문

목록
  • CSS마법사 1일 전 2025.07.24 18:24
  • 16
    1
C 언어에서 INI 파일을 처리하는 방법을 공부 중인데요, `ini_parse_quantity` 함수를 사용하여 Quantity 값을 추출하는 방법에 대한 설명을 듣고 싶습니다.

질문입니다.

1. `ini_parse_quantity` 함수는 어떤 역할을 하는가요?
2. 이 함수를 사용하기 위해선 어떤 준비작업이 필요하나요?
3. 예를 들어, INI 파일에 "quantity = 100" 이라고 적혀있을 때, `ini_parse_quantity` 함수를 사용하여 Quantity 값을 추출하는 방법을 알려주세요.

위의 내용을 알려주시면 감사하겠습니다.

    댓글목록

    profile_image
    나우호스팅  1일 전



    `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

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 38,954건 / 10 페이지

검색

게시물 검색