strtok

How to use strtok()

拈花ヽ惹草 提交于 2019-11-28 12:58:01
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. Jonathan Leffler Make sure you can identify the limits of what you print when you're printing. Output newlines at the end of printed

strtok wont accept: char *str

主宰稳场 提交于 2019-11-28 11:41:10
strtok wont work correctly when using char *str as the first parameter (not the delimiters string). Does it have something to do with the area that allocates strings in that notation? (which as far as i know, is a read-only area). thanks in advance example: //char* str ="- This, a sample string."; // <---doesn't work char str[] ="- This, a sample string."; // <---works char delims[] = " "; char * pch; printf ("Splitting string \"%s\" into tokens:\n",str); pch = strtok (str,delims); while (pch != NULL) { printf ("%s\n",pch); pch = strtok (NULL, delims); } return 0; In the first case, you pass a

Using fgets() and strtok() to read in a file line-by-line in C?

那年仲夏 提交于 2019-11-28 10:37:13
I'm trying to use fgets and strtok() to read in a file line by line, and create a linked list of each different line of information. Right now, I'm only just putting the information into an array, just to try to figure out how to read the information in correctly, but it's not working right. In the while(fgets) portion, it seems to load everything into the array properly, and prints it out. However, after that loop has executed and I try to print out the whole array, I get really weird results.. which is mostly portions of the last line ONLY, and not complete words or anything for the most

C: correct usage of strtok_r

坚强是说给别人听的谎言 提交于 2019-11-28 06:45:29
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, readLessonS ); int numberNTurma = strtol(readNTurma, NULL, 10); And I'm catching that readNTurma == NULL

How does the strtok function in C work? [duplicate]

こ雲淡風輕ζ 提交于 2019-11-28 05:26:52
This question already has an answer here: How does strtok() split the string into tokens in C? 13 answers I found this sample program which explains the strtok function: #include <stdio.h> #include <string.h> int main () { char str[] ="- This, a sample string."; 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; } However, I don't see how this is possible to work. How is it possible that pch = strtok (NULL, " ,.-"); returns a new token. I mean, we are calling strtok

strtok program crashing

让人想犯罪 __ 提交于 2019-11-28 05:02:11
问题 the program for strtok given on http://www.opengroup.org/onlinepubs/000095399/functions/strtok.html crashes everytime.. #include <string.h> ... char *token; char *line = "LINE TO BE SEPARATED"; char *search = " "; /* Token will point to "LINE". */ token = strtok(line, search); /* Token will point to "TO". */ token = strtok(NULL, search); If I use a char array for variable 'line', it works. i.e. char line[] = "LINE TO BE SEPARATED" works. Kindly explain. 回答1: strtok modifies the input string

Split string into tokens and save them in an array

不羁岁月 提交于 2019-11-28 04:33:22
How to split a string into an tokens and then save them in an array? Specifically, I have a string "abc/qwe/jkh" . I want to separate "/" , and then save the tokens into an array. Output will be such that array[0] = "abc" array[1] = "qwe" array[2] = "jkh" please help me rlib #include <stdio.h> #include <string.h> int main () { char buf[] ="abc/qwe/ccd"; int i = 0; char *p = strtok (buf, "/"); char *array[3]; while (p != NULL) { array[i++] = p; p = strtok (NULL, "/"); } for (i = 0; i < 3; ++i) printf("%s\n", array[i]); return 0; } You can use strtok() char string[]= "abc/qwe/jkh"; char *array

Does strtok work with strings (as the delimiter)? [closed]

丶灬走出姿态 提交于 2019-11-28 02:29:57
For example: Friendly. I don't like the "ly" at the end of the word. Can I tokenize this string by "ly" someCharVariable = strtok("friendly", "ly")? The answer is no. Your example of "ly" will delimit on any occurrence of either "l" or "y" or "yl" or "ly" The delimiter parameter is an array of characters, each meant to act as a delimiter. This is an example of what you asked for: char *iterate(char *p, const char *d, const size_t len) { while(p!=NULL && *p && memcmp(p, d, len)==0) { memset(p, 0x0, len); p+=len; } return p; } char ** tokenize( char **result, char *working, const char *src,

Is strtok broken? Or just tricky?

半世苍凉 提交于 2019-11-28 01:46:54
Is strtok hopelessly broken? On many StackOverflow questions about text-parsing in C, someone will suggest using strtok , and one common reply is that strtok should never be used, that it is hopelessly broken. Some posters have claimed that strtok 's problems are limited to multi-threading issues, and it is safe in a single-threaded environment. What is the right answer? Does it work? Is it hopelessly broken? Can you back up your answer with examples? Yes, strtok is hopelessly broken, even in a simple single-threaded program , and I will demonstrate this failure with some sample code: Let us

What's the difference between strtok_r and strtok_s in C?

笑着哭i 提交于 2019-11-27 15:43:00
问题 I'm trying to use this function in a C program that needs to be able to compile in Linux and Windows. At first I tried using strtok_r, but then when I compiled on windows, it complained about the function not existing and said it would assume it's an extern function, but then failed. I then used strtok_s and it compiled! Then I tried on Linux but now it's complaining that there is an "undefined reference to 'strtok_s'". Is one a windows only function and the other a linux function??? What can