【发布时间】: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);
}
【问题讨论】: