我的程序在调用strtok(NULL,“ \ r \ n”);时遇到问题;即使仍有更多标记,在我进行函数调用后仍返回NULL。我已经研究了很长一段时间,无法弄清楚此函数调用会改变随后的strtok()调用的行为。我将非常感谢任何能提供帮助的人。干杯。
主要功能:
int main()
{
char raw[] = "0 4 96 30\r\n3 4 64 60\r\n3 5 64 20\r\n3 2 32 40\r\n5 1 100 20\r\n20 3 4 30\r\n";
char* line = strtok(raw, "\r\n"); //line == "0 4 96 30" OK
line = strtok(NULL, "\r\n"); //line == "3 4 64 60" OK
line = strtok(NULL, "\r\n"); //line == "3 5 64 20" OK
struct Process current = parseProcess(line); //Now strtok calls after this will return NULL...
line = strtok(NULL, "\r\n"); //line == NULL (supposed to be "3 2 32 40")
line = strtok(NULL, "\r\n"); //line == NULL
return 0;
}
使用的结构和功能:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Process {
long timeArrived;
long processId;
long memorySizeReq;
long jobTime;
long remainingTime;
};
//Rips values from the input and puts it into a struct
struct Process parseProcess(char* input){
struct Process output;
//Makes a back up as to not mutate input
char* temp = malloc(strlen(input) * sizeof(char));
strcpy(temp, input);
char* token = strtok(temp, " ");
//Fills out the fields of the parsed output
for(int i=0; i<4; i++){
switch(i){
case 0:
output.timeArrived = atoi(token);
token = strtok(NULL, " ");
break;
case 1:
output.processId = atoi(token);
token = strtok(NULL, " ");
break;
case 2:
output.memorySizeReq = atoi(token);
token = strtok(NULL, " ");
break;
case 3:
output.jobTime = atoi(token);
output.remainingTime = atoi(token);
token = strtok(NULL, " ");
break;
}
}
return output;
}
它可以,但是POSIX strtok_r
在Windows中不起作用,但是您可以使用strtok_s
来解决您的问题:
strtok_s
输出:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Process {
long timeArrived;
long processId;
long memorySizeReq;
long jobTime;
long remainingTime;
};
//Rips values from the input and puts it into a struct
struct Process parseProcess(char* input){
struct Process output;
char* strmax;
//Makes a back up as to not mutate input
char* temp = malloc(strlen(input) * sizeof(char));
strcpy(temp, input);
char* token = strtok_s(temp, " ", &strmax);
//Fills out the fields of the parsed output
for(int i=0; i<4; i++){
switch(i){
case 0:
output.timeArrived = atoi(token);
token = strtok_s(NULL, " ", &strmax);
break;
case 1:
output.processId = atoi(token);
token = strtok_s(NULL, " ", &strmax);
break;
case 2:
output.memorySizeReq = atoi(token);
token = strtok_s(NULL, " ", &strmax);
break;
case 3:
output.jobTime = atoi(token);
output.remainingTime = atoi(token);
token = strtok_s(NULL, " ", &strmax);
break;
}
}
return output;
}
int main()
{
char raw[] = "0 4 96 30\r\n3 4 64 60\r\n3 5 64 20\r\n3 2 32 40\r\n5 1 100 20\r\n20 3 4 30\r\n";
char* strmax;
char* line = strtok_s(raw, "\r\n", &strmax); //line == "0 4 96 30" OK
printf("%s\n", raw); //test prints
line = strtok_s(NULL, "\r\n", &strmax); //line == "3 4 64 60" OK
printf("%s\n", line);
line = strtok_s(NULL, "\r\n", &strmax);
printf("%s\n", line); //line == "3 5 64 20" OK
struct Process current = parseProcess(line); //Now strtok_s calls after, &strmax this will return NULL...
line = strtok_s(NULL, "\r\n", &strmax);
printf("%s\n", line); //line == NULL (supposed to be "3 2 32 40")
line = strtok_s(NULL, "\r\n", &strmax); //line == NULL
printf("%s\n", line);
return 0;
}