Tag: strtok

strtok – char数组与char指针

可能重复: strtok不接受:char * str 使用strtok函数时,使用char *而不是char []导致分段错误。 这运行正常: char string[] = “hello world”; char *result = strtok(string, ” “); 这会导致分段错误: char *string = “hello world”; char *result = strtok(string, ” “); 任何人都能解释导致这种行为差异的原因吗?

strtok未处理的exception;访问冲突写入位置

#include #include #include char *matrix[10][10]; int main(void) { int i; char *list[4]; char *words[20] = { ” cat “, ” car “, ” bear “, ” ship “, ” mouse “, ” beatle “, ” coat “, ” nest “, ” ice “, ” sugar “, ” bacon “, ” frown “, ” smile “, […]

Strtok用法,代码不能正常工作

我正在尝试使用strtok() 。 以下是我写的一段代码。 它不起作用,但无限打印”, ‘” 。 #include #include #include int main(){ char str[]=”this, by the way, is a ‘sample'”; char *tokens; tokens = strtok(str, “, ‘”); //printf(“%s\n”,tokens); //printf(“%s\n”, str); while(tokens!=NULL) { printf(“%s\n”, tokens); tokens = (NULL, “, ‘”); } return 0; } 以下是来自strtok()手册页的代码,该代码完全正常。 #include #include int main () { char str[] =”- This, a sample […]

C将输入文本文件解析为单词

我试图解析输入文件(包含具有多行和分隔符的文本文档,即“!,。?”)为单词。 我的function’分裂function’是: int splitInput(fp) { int i= 0; char line[255]; char *array[5000]; int x; while (fgets(line, sizeof(line), fp) != NULL) { array[i] = strtok(line, “,.!? \n”); printf(“Check print – word %i:%s:\n”,i, array[i]); i++; } return 0; }

错误:赋值在C Prog中从Integer发出没有强制转换的指针

每当我运行程序“Assignment在没有强制转换的情况下从Integer发出指针”时,我都会收到此错误。 我的代码写在下面….请帮助…谢谢 struct student { char studentID[6]; char name[31]; char course [6]; }; struct student *array[MAX]; struct student dummy; int recordCtr=0; int read(){ FILE *stream = NULL; int ctr; char linebuffer[45]; char delims[]=”, “; char *number[3]; char *token = NULL; stream = fopen(“student.txt”, “rt”); if (stream == NULL) stream = fopen(“student.txt”, “wt”); else { printf(“\nReading […]

奇怪的strtok行为

char line[255]; char *token = NULL; char *line2 = NULL; char *temporaryToken = NULL; if( scanf(” %[^\n]”, line) > 0) token = strtok( line, “;” ); //divide the line by ; do { line2 = token; temporaryToken = strtok(line2, ” “); do { //divide the line2 by spaces into command and args, not the question […]

strtok在同时处理两个字符串时

C语言中的新function并且非常混淆了如何使用strtok同时处理多个字符串,简单来说,我想使用strtok提取数字并进行比较。 #include #include int main() { char s1[100]=”11.54″; char s2[100]=”12.55″; const char tok[2]=”.”; char* token1=strtok(s1,tok); char* token2=strtok(s2,tok); while(token1 !=NULL && token2 !=NULL){ int temp=strcmp(token1,token2); if(temp==0){ token1=strtok(NULL,tok); token2=strtok(NULL,tok); } else if(temp<0){ printf("%d\n",-1); return; } else{ printf("%d\n",1); return; } } if(token1 !=NULL){ printf("%d\n",1); return; } if(token2 !=NULL){ printf("%d\n",-1); return; } printf("%d\n",0); return 0; } 但是当我使用strtok时,strtok(NULL,token)将指向当前字符串并将执行如下操作:11-> 12> 55-> […]

strtok在呼叫中的问题

我有一个像这样使用strtok的函数 void f1(char *name) { … char *tmp; tmp = strtok(names, ” ,”); while(tmp) { … tmp = strtok(NULL, ” ,”); } … } 我有一个电话f1(“abc,def”); 问题是,在第一次调用f1获得abc时,def和第二次调用只获得abc 我很困惑..为什么会这样?

从strtok()获取零长度字符串

我有一个包含数据的CSV文件 value;name;test;etc 我试图通过使用strtok(string, “;”)进行拆分。 但是,此文件可以包含零长度数据,如下所示: value;;test;etc 哪个strtok()跳过。 有没有办法可以避免strtok像这样跳过零长度数据?

函数’strtok_r’的隐式声明尽管包括

我有以下代码来标记包含由\n分隔的行的字符串,每行包含由\t分隔的整数: void string_to_int_array(char file_contents[BUFFER_SIZE << 5], int array[200][51]) { char *saveptr1, *saveptr2; char *str1, *str2; char delimiter1[2] = "\n"; char delimiter2[2] = " "; char line[200]; char integer[200]; int j; for(j = 1, str1 = file_contents; ; j++, str1 = NULL) { line = strtok_r(str1, delimiter1, &saveptr1); if (line == NULL) { break; } for […]