C语言 我的if语句只输入一次

kmbjn2e3  于 2023-03-29  发布在  其他
关注(0)|答案(1)|浏览(126)

我目前正在处理我的作业,我严重卡住了。我面临的问题是我的链表在文件Reader函数中。我有if语句:if to_airport_country != NULL && strncmp("Canada", to_airport_country, 6) == 0)是给我的问题.它只进入一次,然后再也没有.如果有人可以请看看我的代码,让我知道我做错了,这将是惊人的.
下面是我的完整代码:

/** @file route_manager.c
 *  @brief A small program to analyze airline routes data.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
#include "emalloc.h"

// TODO: Make sure to adjust this based on the input files given
#define MAX_LINE_LEN 80

/**
 * @brief Serves as an incremental counter for navigating the list.
 *
 * @param p The pointer of the node to print.
 * @param arg The pointer of the index.
 *
 */
void inccounter(node_t *p, void *arg)
{
    int *ip = (int *)arg;
    (*ip)++;
}

/**
 * @brief Allows to print out the content of a node.
 *
 * @param p The pointer of the node to print.
 * @param arg The format of the string.
 *
 */
void print_node(node_t *p, void *arg)
{
    char *fmt = (char *)arg;
    printf(fmt, p->subject);
}

/**
 * @brief Allows to print each node in the list.
 *
 * @param l The first node in the list
 *
 */
void analysis(node_t *l)
{
    int len = 0;

    apply(l, inccounter, &len);
    printf("Number of words: %d\n", len);

    apply(l, print_node, "%s\n");
}

node_t* find_in_list(node_t *list, char *searched) {
    node_t *curr = list;
    while (curr != NULL) {
        if (strcmp(curr->subject, searched) == 0) {
            return curr;
        }
        curr = curr->next;
    }
    return NULL;
}

char *extract_value_after_colon(char *input) {
    char *value_start = strstr(input, ": ");
    if (value_start != NULL) {
        value_start += 2; 
        int value_len = strlen(value_start);
        char *value = (char *)malloc(value_len + 1);
        strncpy(value, value_start, value_len);
        value[value_len] = '\0';
        return value;
    }
    return NULL;
}

int fileReader(char *DATA, char *QUESTION, char *VALUE, node_t **list) {
    
    char input[128];
    FILE *fp = fopen(DATA, "r");

    if (!fp) {
        printf("Failed to open file.\n");
        return 1;
    }

    char *airline_name;
    char *airline_icao_unique_code;
    char *airline_country;
    char *from_airport_name;
    char *from_airport_city;
    char *from_airport_country;
    char *from_airport_icao_unique_code;
    char *from_airport_altitude;
    char *to_airport_name;
    char *to_airport_city;
    char *to_airport_country;
    char *to_airport_icao_unique_code;
    char *to_airport_altitude;

    // skips over first line
    if (fgets(input, sizeof(input), fp) == NULL) {
        perror("Error reading file");
        return 1;
    }

    // checker to see when we should stop the loop
    int checker = 0;

    // iterates over the YAML file
    while (checker != 1) {
        
        to_airport_country = NULL;

        // iterates over every 13 lines
        for (int i = 0; i < 13; i++) {
            if (fgets(input, sizeof input, fp) == NULL){
            checker = 1;
            }

            // gets length of the input
            int len = strlen(input);

            // checks to see if the "-" is at the front and removes it
            if (strncmp("-", input, 1) == 0) {
                memcpy(input, "  ", 2);
            }

            // checks which variable is associated with which key
            if (strncmp("  airline_name: ", input, 13) == 0) {
                airline_name = extract_value_after_colon(input);
            } else if (strncmp("  airline_icao_unique_code:", input, 27) == 0) {
                airline_icao_unique_code = extract_value_after_colon(input);
            } else if (strncmp("  airline_country:", input, 18) == 0) {
                airline_country = extract_value_after_colon(input);
            } else if (strncmp("  from_airport_name:", input, 20) == 0) {
                from_airport_name = extract_value_after_colon(input);
            } else if (strncmp("  from_airport_city:", input, 20) == 0) {
                from_airport_city = extract_value_after_colon(input);
            } else if (strncmp("  from_airport_country:", input, 23) == 0) {
                from_airport_country = extract_value_after_colon(input);
            } else if (strncmp("  from_airport_icao_unique_code:", input, 32) == 0) {
                from_airport_icao_unique_code = extract_value_after_colon(input);
            } else if (strncmp("  from_airport_altitude:", input, 24) == 0) {
                from_airport_altitude = extract_value_after_colon(input);
            } else if (strncmp("  to_airport_name:", input, 18) == 0) {
                to_airport_name = extract_value_after_colon(input);
            } else if (strncmp("  to_airport_city:", input, 18) == 0) {
                to_airport_city = extract_value_after_colon(input);
            } else if (strncmp("  to_airport_country:", input, 21) == 0) {
                to_airport_country = extract_value_after_colon(input);
            } else if (strncmp("  to_airport_icao_unique_code:", input, 30) == 0) {
                to_airport_icao_unique_code = extract_value_after_colon(input);
            } else if (strncmp("  to_airport_altitude:", input, 22) == 0) {
                to_airport_altitude = extract_value_after_colon(input);
            }
        }
        if (to_airport_country != NULL && strncmp("Canada", to_airport_country, 6) == 0) {
            node_t *result = find_in_list(*list, airline_name);
            printf("lol");
            if (result != NULL) {
                printf("Found node with data: %s\n", result->subject);
            } else {
                struct node_t *new_node = malloc(sizeof(struct node_t));
                strcpy(new_node->subject, airline_name);
                new_node->statistic = 1;
                new_node->next = NULL;
                if (*list == NULL) {
                    *list = new_node;
                } else {
                    new_node->next = *list;
                    *list = new_node;
                }
            }
        }
        free(airline_name);
        free(airline_icao_unique_code);
        free(airline_country);
        free(from_airport_name);
        free(from_airport_city);
        free(from_airport_country);
        free(from_airport_icao_unique_code);
        free(from_airport_altitude);
        free(to_airport_name);
        free(to_airport_city);
        free(to_airport_country);
        free(to_airport_icao_unique_code);
        free(to_airport_altitude);
    }
    fclose(fp);
    return 0;
}

int argument_checker(int argc, char *argv[], char *DATA, char *QUESTION, char *VALUE, node_t **list) {

    char * argument;
    int selector;

    // Checks Edge cases
    if (argc != 4){
        printf("You entered the incorrect amount of argumnents"); //if 3 arguments are not entered
        return 1;
    }

    int i;
    for (i = 1; i < argc; i++){
        argument = strtok(argv[i], "=");
        
        if (strcmp(argument, "--DATA") == 0) {
            DATA = strtok(NULL, "=");

        } else if (strcmp(argument, "--QUESTION") == 0){
            QUESTION = strtok(NULL, "=");

        } else if (strcmp(argument, "--N") == 0){
            VALUE = strtok(NULL, "=");

        } else {
            printf("An incorrect argument was passed");
            return 1;
        }
    }

    printf("%s", VALUE);
    return fileReader(DATA, QUESTION, VALUE, list);
}

/**
 * @brief The main function and entry point of the program.
 *
 * @param argc The number of arguments passed to the program.
 * @param argv The list of arguments passed to the program.
 * @return int 0: No errors; 1: Errors produced.
 *
 */
int main(int argc, char *argv[])
{

    char * DATA = NULL;
    char * QUESTION = NULL;
    char * VALUE = NULL;

    node_t *list = NULL;

    int result = argument_checker(argc, argv, DATA, QUESTION, VALUE, &list);

    exit(result);
}
csga3l58

csga3l581#

我发现了问题,似乎是strcpy(new_node->subject, airline_name);行导致了我的代码出现问题。
感谢所有帮助!

相关问题