strtok

Need to know when no data appears between two token separators using strtok()

早过忘川 提交于 2019-11-27 02:10:41
I am trying to tokenize a string but I need to know exactly when no data is seen between two tokens. e.g when tokenizing the following string " a,b,c,,,d,e " I need to know about the two empty slots between ' d ' and ' e '... which I am unable to find out simply using strtok() . My attempt is shown below: char arr_fields[num_of_fields]; char delim[]=",\n"; char *tok; tok=strtok(line,delim);//line contains the data for(i=0;i<num_of_fields;i++,tok=strtok(NULL,delim)) { if(tok) sprintf(arr_fields[i], "%s", tok); else sprintf(arr_fields[i], "%s", "-"); } Executing the above code with the

How to split a string to 2 strings in C

谁说我不能喝 提交于 2019-11-27 00:05:35
I was wondering how you could take 1 string, split it into 2 with a delimiter, such as space, and assign the 2 parts to 2 separate strings. I've tried using strtok() but to no avail. ereOn #include <string.h> char *token; char line[] = "SEVERAL WORDS"; char *search = " "; // Token will point to "SEVERAL". token = strtok(line, search); // Token will point to "WORDS". token = strtok(NULL, search); Update Note that on some operating systems, strtok man page mentions: This interface is obsoleted by strsep(3). An example with strsep is shown below: char* token; char* string; char* tofree; string =

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

℡╲_俬逩灬. 提交于 2019-11-26 23:44:07
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . 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")? 回答1: The answer is no. Your example of "ly" will delimit on any

What are the differences between strtok and strsep in C

十年热恋 提交于 2019-11-26 20:42:55
Could someone explain me what differences there are between strtok() and strsep() ? What are the advantages and disadvantages of them? And why would I pick one over the other one. George Gaál From The GNU C Library manual - Finding Tokens in a String : One difference between strsep and strtok_r is that if the input string contains more than one character from delimiter in a row strsep returns an empty string for each pair of characters from delimiter. This means that a program normally should test for strsep returning an empty string before processing it. One major difference between strtok()

strtok segmentation fault

ε祈祈猫儿з 提交于 2019-11-26 17:53:07
I am trying to understand why the following snippet of code is giving a segmentation fault: void tokenize(char* line) { char* cmd = strtok(line," "); while (cmd != NULL) { printf ("%s\n",cmd); cmd = strtok(NULL, " "); } } int main(void) { tokenize("this is a test"); } I know that strtok() does not actually tokenize on string literals, but in this case, line points directly to the string "this is a test" which is internally an array of char . Is there any of tokenizing line without copying it into an array? The problem is that you're attempting to modify a string literal. Doing so causes your

Using strtok in c

一个人想着一个人 提交于 2019-11-26 16:45:03
I need to use strtok to read in a first and last name and seperate it. How can I store the names where I can use them idependently in two seperate char arrays? #include <stdio.h> #include <string.h> int main () { char str[] ="test string."; char * test; test = strtok (str," "); while (test != NULL) { printf ("%s\n",test); test= strtok (NULL, " "); } return 0; } Here is my take at a reasonably simple tokenize helper that stores results in a dynamically growing array null-terminating the array keeps the input string safe (strtok modifies the input string, which is undefined behaviour on a

Using strtok() in a loop in C?

穿精又带淫゛_ 提交于 2019-11-26 16:41:59
I am trying to use strtok() in nested loop. But this is not giving me desired results. Possibly because they are using same memory location. My code is of the form:- char *token1 = strtok(Str1, "%"); while(token1 != NULL ) { char *token2 = strtok(Str2, "%"); while(token2 != NULL ) { //DO SMTHING token2 = strtok(NULL, "%"); } token1 = strtok(NULL, "%"); // Do something more } Yes, strtok() , indeed, uses some static memory to save its context between invocations. Use a reentrant version of strtok() , strtok_r() instead, or strtok_s() if you are using VS (identical to strtok_r() ). It has an

Why is strtok() Considered Unsafe?

拜拜、爱过 提交于 2019-11-26 15:27:35
What feature(s) of strtok is unsafe (in terms of buffer overflow) that I need to watch out for? What's a little weird to me is that strtok_s (which is "safe") in Visual C++ has an extra "context" parameter, but it looks like it's the same in other ways... is it the same, or is it actually different? According with the strtok_s section of this document : 6.7.3.1 The strtok_s function The strtok_s function fixes two problems in the strtok function: A new parameter, s1max, prevents strtok_s from storing outside of the string being tokenized. (The string being divided into tokens is both an input

strtok causing segfault but not when step through code

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 14:56:42
问题 I am new to C and I am trying to split a date/time string into separate variables. However, when I step through the code in gdb line by line, it works, however, when I let it run through normally without breakpoints it seg faults and I can't see why. Below is the code: char * dateTimeString = "2011/04/16 00:00"; char dateVar[11]; char timeVar[6]; if (splitTimeAndDateString(dateVar, timeVar, dateTimeString)) { exit(1); } printf("Date: %s\tTime: %s\n", dateVar, timeVar); Below is the function

Nested strtok function problem in C [duplicate]

戏子无情 提交于 2019-11-26 14:43:16
This question already has an answer here: Using strtok() in a loop in C? 3 answers I have a string like this: a;b;c;d;e f;g;h;i;j 1;2;3;4;5 and i want to parse it element by element. I used nested strtok function but it just splits first line and makes null the token pointer. How can i overcome this? Here is the code: token = strtok(str, "\n"); while(token != NULL && *token != EOF) { char a[128], b[128]; strcpy(a,token); strcpy(b,a); printf("a:%s\n",a); char *token2 = strtok(a,";"); while(token2 != NULL) { printf("token2 %s\n",token2); token2 = strtok(NULL,";"); } strcpy(token,b); token =