问题描述
我在每个 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,&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,&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,&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,&ptr);
printf_s("the colsize you selcted is %s\n",col);
// return 0; // if function return type is int
}
另一个问题(尽管不是主要问题)是该函数具有 void
返回类型并返回一个 int
。