Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何通过省略字符串来读取带有逗号的字符串%[^,]不适用于我_C_Arrays_String_Scanf_Calloc - Fatal编程技术网

C 如何通过省略字符串来读取带有逗号的字符串%[^,]不适用于我

C 如何通过省略字符串来读取带有逗号的字符串%[^,]不适用于我,c,arrays,string,scanf,calloc,C,Arrays,String,Scanf,Calloc,我想要的是 Row, row, row your boat, and so on 我尝试过%[^,.\n],但它对我无效。它打印垃圾您可能会发现该功能特别有用。它会将您的字符串拆分为标记,就像C等价于split()或explode() 例如: Row row row your boat #包括 #包括 int main(){ char str[]=“划,划,划你的船,轻轻地顺流而下。”; char*pch; printf(“将字符串\%s\”拆分为标记:\

我想要的是

  Row,
    row,
    row
    your
    boat, and so on
我尝试过%[^,.\n],但它对我无效。它打印垃圾

您可能会发现该功能特别有用。它会将您的字符串拆分为标记,就像C等价于
split()
explode()

例如:

Row
row
row
your
boat
#包括
#包括
int main(){
char str[]=“划,划,划你的船,轻轻地顺流而下。”;
char*pch;
printf(“将字符串\%s\”拆分为标记:\n”,str);
pch=strtok(str,“,.”);
while(pch!=NULL){
printf(“%s\n”,pch);
pch=strtok(空,“,”);
}
返回0;
}

我基本上复制了手册页示例。用第一个参数NULL再次调用该函数将产生下一个字符串标记。

那么,您是否试图用逗号分隔输入字符串?您不需要在C程序中强制转换
calloc()
的返回值。@H2CO3我试图根本不读取逗号。@CarlNorum我不明白您的意思。@KexyKathe Carl Norum
Row
row
row
your
boat
#include <stdio.h>
#include <string.h>

int main (){
  char str[] ="Row, row, row your boat, Gently down the stream.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.");
  while (pch != NULL){
     printf ("%s\n",pch);
     pch = strtok (NULL, " ,.");
  }
  return 0;
}