strtok_s 忽略第一个字符
strtok_s ignors first characters
我正在尝试获取一个输入并将其转换为四个变量名称。
我在每 strtok_s
之后进行检查以查看我得到了什么,但第一个单词只在 4 个字符之后才算。
我的代码:
void zeros()
{
char buffer[81];
fgets(buffer, sizeof(buffer), stdin);
printf("%s\n", buffer);
char *command = strtok_s(buffer, " \t", &buffer);
printf_s("the command you selcted is %s\n", command);
char *Matname = strtok_s(NULL, " \t", &buffer);
printf_s("the name you selcted is %s\n", Matname);
char *row = strtok_s(NULL, " \t", &buffer);
printf_s("the rowsize you selcted is %s\n", row);
char *col = strtok_s(NULL, " \t", &buffer);
printf_s("the colsize you selcted is %s\n", col);
return 0;
}
strtok_s
的最后一个参数不正确,应该是strtok_s
用来存储其内部状态的指针。
您的函数应该更像这样:
void zeros() // or int zeros() if it's supposed to return int
{
char buffer[81];
char *ptr;
fgets(buffer, sizeof buffer, stdin);
printf("%s\n", buffer);
char *command = strtok_s(buffer, " \t", &ptr);
printf_s("the command you selcted is %s\n", command);
char *Matname = strtok_s(NULL, " \t", &ptr);
printf_s("the name you selcted is %s\n", Matname);
char *row = strtok_s(NULL, " \t", &ptr);
printf_s("the rowsize you selcted is %s\n", row);
char *col = strtok_s(NULL, " \t", &ptr);
printf_s("the colsize you selcted is %s\n", col);
// return 0; // if function return type is int
}
另一个问题,尽管不是主要问题,是该函数具有 void
return 类型和 return 一个 int
.
我正在尝试获取一个输入并将其转换为四个变量名称。
我在每 strtok_s
之后进行检查以查看我得到了什么,但第一个单词只在 4 个字符之后才算。
我的代码:
void zeros()
{
char buffer[81];
fgets(buffer, sizeof(buffer), stdin);
printf("%s\n", buffer);
char *command = strtok_s(buffer, " \t", &buffer);
printf_s("the command you selcted is %s\n", command);
char *Matname = strtok_s(NULL, " \t", &buffer);
printf_s("the name you selcted is %s\n", Matname);
char *row = strtok_s(NULL, " \t", &buffer);
printf_s("the rowsize you selcted is %s\n", row);
char *col = strtok_s(NULL, " \t", &buffer);
printf_s("the colsize you selcted is %s\n", col);
return 0;
}
strtok_s
的最后一个参数不正确,应该是strtok_s
用来存储其内部状态的指针。
您的函数应该更像这样:
void zeros() // or int zeros() if it's supposed to return int
{
char buffer[81];
char *ptr;
fgets(buffer, sizeof buffer, stdin);
printf("%s\n", buffer);
char *command = strtok_s(buffer, " \t", &ptr);
printf_s("the command you selcted is %s\n", command);
char *Matname = strtok_s(NULL, " \t", &ptr);
printf_s("the name you selcted is %s\n", Matname);
char *row = strtok_s(NULL, " \t", &ptr);
printf_s("the rowsize you selcted is %s\n", row);
char *col = strtok_s(NULL, " \t", &ptr);
printf_s("the colsize you selcted is %s\n", col);
// return 0; // if function return type is int
}
另一个问题,尽管不是主要问题,是该函数具有 void
return 类型和 return 一个 int
.