Questions tagged [strtok]

strtok() is a Standard C (ISO 9899:1989) function for splitting a string in tokens. strtok_r() is the thread-safe variant defined by IEEE Std 1003.1:2004 (aka "POSIX").

NAME

strtok, strtok_r - split string into tokens

SYNOPSIS

#include <string.h>

char *strtok(char *restrict s1, const char *restrict s2);
char *strtok_r(char *restrict s, const char *restrict sep,
       char **restrict lasts);
1404 questions
4
votes
1 answer

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

I am using strtok() function to split a string into Tokens.The problem is when there are 2 delimiters in row. /* strtok example */ #include #include int main () { char str[] ="Test= 0.28,0.0,1,,1.9,2.2,1.0,,8,4,,,42,,"; …
iSwear
  • 53
  • 2
  • 6
4
votes
1 answer

In C, strtok causing my program to crash even when compiling example code from different sources

using the code demonstrated below, the program crashes. It is a code I copy-pasted from this source and haven't modified it at all, yet strtok seems to cause the program to crash. #include #include int main() { char str[80]…
Nivp
  • 41
  • 6
4
votes
4 answers

Freeing memory after calling strtok() causes an error

Could you please help me? My code does tokenizing, so I created code like this: I allocate some memory, I strcpy(malloced_memory, argv) I execute strtok(mallocted_memory, ".") Try free(mallocted_memory). filename =…
wh.Kwon
  • 51
  • 2
4
votes
4 answers

Memory leak when splitting sentences with strtok

I'm trying to split a string into sentences (delimited by sentence delimiters). The code itself it working but I keep getting memory leaks in the function. char ** splitSentences(char *string) { int sentencecount = 0; char* buf = NULL; char* str =…
Moox
  • 1,122
  • 9
  • 23
4
votes
1 answer

strtok with multiple delimiters

I tried to parse a string like: "12 13 14 16" for 5 numbers in an array. I use strtok(string_above, " "), but strtok() will take these three blank characters as one. What can I do to prevent it?
KennyYang
  • 235
  • 2
  • 13
4
votes
2 answers

get the last token of a string in C

what I want to do is given an input string, which I will not know it's size or the number of tokens, be able to print it's last token. e.x.: char* s = "some/very/big/string"; char* token; const char delimiter[2] = "/"; token = strtok(s,…
Jack
  • 722
  • 3
  • 8
  • 24
4
votes
2 answers

Read .ini file and return specific value in C

So, I need to access a .ini file like: [alpha] colour=green size=78 style=italic [beta] colour=black size=94 mode=xyz [gamma] black=0231 blue=127 red=0x35876f I need to find a section [likethis], then a parameter (one of the follow three) and then…
rockstiff
  • 355
  • 1
  • 2
  • 17
4
votes
8 answers

How to remove first word from a string?

Let's say I have string sentence{"Hello how are you."} And I want string sentence to have "how are you" without the "Hello". How would I go about doing that. I tried doing something like: stringstream ss(sentence); ss>> string junkWord;//to get…
user3247278
  • 179
  • 2
  • 3
  • 13
4
votes
1 answer

Strtok to separate all whitespace

I'm trying to split a string at spaces and tabs. char * token = strtok(input, " \t"); works only for spaces. What am I doing wrong?
Haskell
  • 367
  • 3
  • 7
  • 15
4
votes
2 answers

How to use strtok()

I'm writing a C program to study the usage of function strtok(). Here is my code: #include #include main() { char abc[100] = "ls &"; char *tok; tok = strtok(abc, " "); while (tok != NULL) { printf("%s",…
user2201650
  • 527
  • 7
  • 13
  • 28
4
votes
5 answers

String parsing in C using strtok

I've got this little source code, made for testing the parsing of a string similar to variable string I need to use in other project #include #include #include int main (void) { char string[] =…
Arrigo Pierotti
  • 203
  • 4
  • 10
  • 18
4
votes
2 answers

strtok_r causing "assignment makes pointer from integer without a cast"

I'm attempting to tokenize a String in C and save the tokens into multiple variables using strtok_r. As far as I can tell, I'm using it exactly as documented: char *saveptr; char *ticketuser = strtok_r(request, ":", &saveptr); char *ticketservice =…
JMcMinn
  • 275
  • 3
  • 10
4
votes
10 answers

C++ How to convert string to char*

I need to convert a string to a char * for use in strtok_s and have been unable to figure it out. c_str() converts to a const char *, which is incompatible. Also, if someone could explain to me why the second strtok_s function (inside the loop) is…
Nona Urbiz
  • 4,873
  • 16
  • 57
  • 84
4
votes
1 answer

Using strcpy with std::vector

I have some trouble using strcpy with a vector of instances of my own class. Here's the class: class elemente { char name[5]; short val; bool red; }; So, I made a vector from this class: vector ele(1); But if I…
StefanEuSunt
  • 133
  • 1
  • 6
4
votes
2 answers

Reading and parsing lines from a file with fgets and strtok

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…
user2100887
  • 43
  • 1
  • 3