strtok

strange strtok behaviour

女生的网名这么多〃 提交于 2019-12-20 04:28:04
问题 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 here] temporaryToken = strtok( NULL, " " ); }while (temporaryToken != NULL ); token = strtok( NULL, ";" ); }while(token != NULL); this is not my code verbatim, by the way, just an example of how it

strtok when process two strings at same time

会有一股神秘感。 提交于 2019-12-20 03:21:28
问题 New in C and pretty confused about how to deal with several strings at the same time using strtok, for a simply example, I want to use strtok to extract the number and compare then. #include <stdio.h> #include <string.h> 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); }

strtok when process two strings at same time

回眸只為那壹抹淺笑 提交于 2019-12-20 03:21:06
问题 New in C and pretty confused about how to deal with several strings at the same time using strtok, for a simply example, I want to use strtok to extract the number and compare then. #include <stdio.h> #include <string.h> 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); }

Split string into Tokens in C, when there are 2 delimiters in a row

情到浓时终转凉″ 提交于 2019-12-19 10:35:07
问题 I am using strtok() function to split a string into Tokens.The problem is when there are 2 delimiters in row. /* strtok example */ #include <stdio.h> #include <string.h> int main () { char str[] ="Test= 0.28,0.0,1,,1.9,2.2,1.0,,8,4,,,42,,"; char * pch; printf ("Splitting string \"%s\" into tokens:\n",str); pch = strtok (str,", "); while (pch != NULL) { printf ("Token = %s\n",pch); pch = strtok (NULL, ", "); } return 0; } And outputs: Splitting string "Test= 0.28,0.0,1,,1.9,2.2,1.0,,8,4,,,42,,

Reading and parsing lines from a file with fgets and strtok

元气小坏坏 提交于 2019-12-19 09:08:09
问题 I'm having trouble with a fairly basic bit of code. I need to read each line from the file shown below, split it up into the 3 parts with strtok, and store each part into an array. The arrays for "goals" and "assists" are working perfectly, but for some reason the entire name array is filled with the last name read from the file. Input file: Redden 2 0 Berglund 5 2 Jackman 2 0 Stewart 4 0 Oshie 3 5 McDonald 2 4 Pietrangelo 2 7 Perron 2 6 Tarasenko 5 5 Relevant code: int main(int argc, char*

C - Unexpected Segmentation Fault on strtok(…)

笑着哭i 提交于 2019-12-19 04:28:09
问题 I am using strtok(...) of the library and it appears to be working fine until the end condition, where it results in a segmentation fault and program crash. The API claims that strtok(...) will output a NULL when there are no more tokens to be found, which meant, I thought, that you had to catch this NULL in order to terminate any loops that you were running using strtok(...). What do I need to do to catch this NULL to prevent my program from crashing? I imagined the NULL was allowed for use

why do we use NULL in strtok()?

一曲冷凌霜 提交于 2019-12-18 12:40:11
问题 Why do we use null in strok() function? while(h!=NULL) { h=strtok(NULL,delim); if(hold!=NULL) printf("%s",hold); } What does this program do when *h is pointing to a string? 回答1: strtok is part of the C library and what it does is splitting a C null-delimited string into tokens separated by any delimiter you specify. The first call to strtok must pass the C string to tokenize, and subsequent calls must specify NULL as the first argument, which tells the function to continue tokenizing the

How to use strtok()

跟風遠走 提交于 2019-12-17 20:39:23
问题 I'm writing a C program to study the usage of function strtok() . Here is my code: #include <stdio.h> #include <string.h> main() { char abc[100] = "ls &"; char *tok; tok = strtok(abc, " "); while (tok != NULL) { printf("%s", tok); tok = strtok(NULL, " "); } printf("\n\n\n\n\n%s", tok); return 0; } It is printing the following output: ls& (null) But I want it to print & at the second printf statement. How do I do it? I need this part for my homework project. 回答1: Make sure you can identify the

Why is strtok changing its input like this?

巧了我就是萌 提交于 2019-12-17 10:50:10
问题 Ok, so I understand that strtok modifies its input argument, but in this case, it's collapsing down the input string into only the first token. Why is this happening, and what can I do to fix it? (Please note, I'm not talking about the variable "temp", which should be the first token, but rather the variable "input", which after one call to strtok becomes "this") #include <string.h> #include <stdlib.h> #include <stdio.h> int main(int argc, char* argv[]) { char input[]="this is a test of the

C: correct usage of strtok_r

允我心安 提交于 2019-12-17 10:47:51
问题 How can I use strtok_r instead of strtok to do this? char *pchE = strtok(NULL, " "); Now I'm trying to use strtok_r properly... But sometimes I get problems with the strtol . I have a thread that I execute 10 times (at the same time). char *savedEndd1; char *nomeClass = strtok_r(lineClasses, " ", &savedEndd1); char *readLessonS = strtok_r (NULL, " ", &savedEndd1); char *readNTurma = strtok_r(NULL, " ", &savedEndd1); if (readNTurma==NULL) printf("CLASS STRTOL begin %s %s\n",nomeClass,