【问题标题】:using strtok to get input from file使用 strtok 从文件中获取输入
【发布时间】:2021-05-12 18:30:30
【问题描述】:

我正在尝试创建一个程序,该程序从文件中获取多个进程(名称、开始时间、剩余时间),然后使用循环算法来处理队列。

问题是,当我尝试使用strtok()fgets() 标记文件的每一行时,进程的名称总是错误的。

例如,如果第一行是P1 0 3,则输出如下:

void RoundRobin(char *filename) {
    Queue *q = initQueue();
    char string[MAX_SIZE];
    FILE *file;
    Process process[20];
    char *token;
    file = fopen(filename, "r");
    if (!file) {
        printf("File Cannot Be Opened");
    }
    fgets(string, 150, file);
    token = strtok(string, "=");
    token = strtok(NULL, "+");
    int time_quantum = atoi(token);
    int process_count = 0;
    while (fgets(string, 150, file)) {
        char *token1;
        token1 = strtok(string, " ");
        process[process_count].name = token1;

        token1 = strtok(NULL, " ");
        process[process_count].starting_time = atoi(token1);

        token1 = strtok(NULL, " ");
        process[process_count++].remaining_time = atoi(token1);
        token1 = strtok(NULL, " ");
    }

    for (int i = 0; i < process_count; i++) {
        printf("%s  %d  %d\n", process[i].name, process[i].starting_time, process[i].remaining_time);
    }

    fclose(file);
}

【问题讨论】:

标签: c string strtok


【解决方案1】:

您正在重复使用单个 char[] 进行所有令牌解析。 fgets() 每次都会覆盖char[] 的内容,strtok() 将返回指向char[] 内部内存的指针。因此,每次您从文件中读取新行时,您已经存储在 process[] 数组中的先前指针仍指向同一内存,但该内存的内容已更改。

您需要为要保存在process[] 数组中的每个name 分配一个单独的char[] 字符串。您可以为此使用strdup(),例如:

while (fgets(string, 150, file)){
    char* token1 token1 = strtok(string, " ");
    process[process_count].name = strdup(token1); // <-- HERE

    ...
}

// use process[] as needed...

for(int i = 0; i < process_count; i++){
    free(process[i].name);
}

【讨论】:

    【解决方案2】:

    问题是strtok() 返回一个指向它解析的行的指针。因此,process 数组中的所有条目都指向同一个 string 数组,该数组由对 fgets() 的调用修改。

    您必须复制您存储在流程描述结构中的字符串:

    void RoundRobin(const char *filename) {
        char string[MAX_SIZE];
        Process process[20];
        Queue *q = initQueue();
        char *token;
        FILE *file;
        file = fopen(filename, "r");
        if (!file) {
            printf("Cannot open file %s\n", filename);
            return;
        }
        int time_quantum = 0;
        int process_count = 0;
    
        if (fgets(string, sizeof string, file)
        &&  (token = strtok(string, "=")) != NULL
        &&  (token = strtok(NULL, "+")) != NULL) {
            time_quantum = atoi(token);
        }
        while (fgets(string, sizeof string, file)) {
            char *token1;
    
            if ((token1 = strtok(string, " ")) == NULL)
                contine;
            process[process_count].name = strdup(token1);
    
            if ((token1 = strtok(string, " ")) == NULL)
                contine;
            process[process_count].starting_time = atoi(token1);
    
            if ((token1 = strtok(string, " ")) == NULL)
                contine;
            process[process_count].remaining_time = atoi(token1);
            process_count++;
        }
    
        for (int i = 0; i < process_count; i++) {
            printf("%s  %d  %d\n", process[i].name, process[i].starting_time, process[i].remaining_time);
        }
        for (int i = 0; i < process_count; i++) {
            free(process[i].name);
        }
        fclose(file);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-06
      • 2019-06-04
      相关资源
      最近更新 更多