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
10
votes
4 answers

Implicit Declaration of Function ‘strtok_r’ Despite Including

I have the following code to tokenize a string containing lines separated by \n and each line has integers separated by a \t: void string_to_int_array(char file_contents[BUFFER_SIZE << 5], int array[200][51]) { char *saveptr1, *saveptr2; char…
jobin
  • 2,600
  • 7
  • 32
  • 59
10
votes
3 answers

C - Determining which delimiter used - strtok()

Let's say I'm using strtok() like this.. char *token = strtok(input, ";-/"); Is there a way to figure out which token actually gets used? For instance, if the inputs was something like: Hello there; How are you? / I'm good - End Can I figure out…
Andrew Backes
  • 1,884
  • 4
  • 21
  • 37
9
votes
6 answers

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

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…
user1126425
  • 2,385
  • 4
  • 18
  • 17
9
votes
3 answers

tokenizing a string twice in c with strtok()

I'm using strtok() in c to parse a csv string. First I tokenize it to just find out how many tokens there are so I can allocate a string of the correct size. Then I go through using the same variable I used last time for tokenization. Every time I…
SummerCodin
  • 263
  • 1
  • 6
  • 10
9
votes
4 answers

c++ tokenize std string

Possible Duplicate: How do I tokenize a string in C++? Hello I was wondering how I would tokenize a std string with strtok string line = "hello, world, bye"; char * pch = strtok(line.c_str(),","); I get the following error error: invalid…
Daniel Del Core
  • 3,071
  • 13
  • 38
  • 52
8
votes
2 answers

strtok on 64 bit machines

The following code works differently on 64 bit and on 32 bit which is causing me trouble to port my code. char * tmp = "How are you?"; printf("size of char * = %ld and size of strtok return val = %ld \n",sizeof(char *),sizeof(strtok(tmp,"…
ntalli
  • 83
  • 6
8
votes
2 answers

Breaking down string and storing it in array

I want to break down a sentence and store each string in an array. Here is my code: #include #include int main(void) { int i = 0; char* strArray[40]; char* writablestring= "The C Programming Language"; char…
Adam Adamou
  • 243
  • 2
  • 6
  • 12
7
votes
5 answers

Double split in C

OK. For example I have this line in my txt file: 1|1,12;7,19;6,4;8,19;2,2 As you can see, it has 2 parts, separated by |. I have no problems getting both parts, and separating second part 1,12;7,19;6,4;8,19;2,2 using ; separator. BUT I do have…
Dmitri
  • 2,451
  • 6
  • 35
  • 55
7
votes
5 answers

Obtaining zero-length string from strtok()

I have a CSV file containing data such as value;name;test;etc which I'm trying to split by using strtok(string, ";"). However, this file can contain zero-length data, like this: value;;test;etc which strtok() skips. Is there a way I can avoid…
Mauren
  • 1,955
  • 2
  • 18
  • 28
7
votes
5 answers

strtok() issue: If tokens are delimited by delimiters,why is last token between a delimiter and the null '\0'?

In the following program, strtok() works as expected in the major part but I just can't comprehend the reason behind one finding. I have read about strtok() that: To determine the beginning and the end of a token, the function first scans from…
Rüppell's Vulture
  • 3,583
  • 7
  • 35
  • 49
6
votes
2 answers

Problem with strtok and segmentation fault

I have two helper functions to break up strings in the format of decimal prices ie. "23.00", "2.30" Consider this: char price[4] = "2.20"; unsigned getDollars(char *price) { return atoi(strtok(price, ".")); } unsigned…
Chris
  • 7,996
  • 11
  • 66
  • 98
6
votes
3 answers

Why does the following C program give a bus error?

I think it's the very first strtok call that's failing. It's been a while since I've written C and I'm at a loss. Thanks very much. #include #include int main(int argc, char **argv) { char *str = "one|two|three"; char *tok…
nc.
  • 7,179
  • 5
  • 28
  • 38
6
votes
3 answers

C : warning: assignment makes pointer from integer without a cast [enabled by default]

This is my code #include #include void main() { FILE *fp; char * word; char line[255]; fp=fopen("input.txt","r"); while(fgets(line,255,fp)){ word=strtok(line," "); while(word){ …
jophab
  • 5,356
  • 14
  • 41
  • 60
6
votes
3 answers

Segmentation fault when using regexec/strtok_r in C

I'm having problems in figuring out where and why I'm receiving a segmentation fault. I'm writing a C code that prompts the user to input a regular expression and compile it and then enter a string with multiple sentences: int main(void){ char…
higz555
  • 115
  • 8
6
votes
5 answers

Split string by a substring

I have following string: char str[] = "A/USING=B)"; I want to split to get separate A and B values with /USING= as a delimiter How can I do it? I known strtok() but it just split by one character as delimiter.
Ryo
  • 995
  • 2
  • 25
  • 41
1 2
3
93 94