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
5
votes
3 answers

strtok() and empty fields

I am serializing some C structure to string and than deserializing it with strtok(). But, unfortunately, strtok() don't detect empty fields (eg 1:2::4). Is there any alternative function?
Marko Kevac
  • 2,902
  • 30
  • 47
5
votes
1 answer

Unexpected strtok() behaviour

I'm trying to count the number of words in a file with strtok(). /* * code.c * * WHAT * Use strtok() to count the number of words in a file. */ #include #include #include #define STRMAX 128 int main() { …
Pieter
  • 31,619
  • 76
  • 167
  • 242
5
votes
5 answers

C: creating array of strings from delimited source string

What would be an efficient way of converting a delimited string into an array of strings in C (not C++)? For example, I might have: char *input = "valgrind --leak-check=yes --track-origins=yes ./a.out" The source string will always have only a…
yavoh
  • 2,645
  • 5
  • 24
  • 21
5
votes
3 answers

strtok how to also include delimiters as tokens

Right now I have code set up to divide up my string into tokens with delimiters of ,;= and space. I would also like to include the special characters as tokens. char * cstr = new char [str.length()+1]; strcpy (cstr, str.c_str()); char * p = strtok…
Andrew Tsay
  • 1,893
  • 6
  • 23
  • 35
5
votes
2 answers

execvp and type of parameters - ansi c

I am having trouble with using execvp(). execvp() expects type char * const* as second parameter. I want to parse arguments passed to application (in argv) and make an array of that type. For example, user is invoking the binary as given…
marxin
  • 3,692
  • 3
  • 31
  • 44
4
votes
2 answers

Want to free my pointer token after strtok

I have extracted the "meaning" part of my code (and also replace some line to simplify it). I have 2 dynamic pointers, one for the current line (extracted from a file) and a second for the current token. Following this question, Free/delete strtok_r…
roro
  • 131
  • 1
  • 2
  • 8
4
votes
2 answers

Confusion using strtok

Im using strtok and getting a little confused. I have an array holding a lot of strings and I want to tokenize the strings into a temporary array. When i perform the strtok it stored the first token in the temporary array, but also changed the…
meriley
  • 1,831
  • 3
  • 20
  • 33
4
votes
4 answers

Reading a file in C

I have an input file I need to extract words from. The words can only contain letters and numbers so anything else will be treated as a delimiter. I tried fscanf,fgets+sscanf and strtok but nothing seems to work. while(!feof(file)) { …
Ihateparsing
  • 55
  • 1
  • 5
4
votes
7 answers

developed a strtok alternative

I have developed my own version of strtok. Just to practice the use of pointers. Can anyone see any limitations with this or anyway I can improve. void stvstrtok(const char *source, char *dest, const char token) { /* Search for the token. */ …
ant2009
  • 27,094
  • 154
  • 411
  • 609
4
votes
3 answers

Error in strtok function in C

I am using a simple program to tokenize a string using strtok function. Here is the code - # include char str[] = "now # time for all # good men to # aid of their country"; //line a char delims[] = "#"; char *result = NULL; result =…
user496934
  • 3,822
  • 10
  • 45
  • 64
4
votes
2 answers

Missing elements when returning a std::vector

I am writing a function in C++ which should, in theory, take user input and splice this input into segments according to whitespace and return these segments as a vector. What I am currently doing is by using strtok() on the input string in order…
yrmy
  • 43
  • 3
4
votes
2 answers

strtok behaving inconsistently

I am trying to read data from a file, tokenize it and sort it, however strtok behaves erratically when I run it, sometimes it works sometimes it doesn't and I get very short/odd tokens. Valgrind seems to think it is because strtok is relying on an…
jrisebor
  • 70
  • 9
4
votes
1 answer

Copying content of strtok token in C

Need to separate a string and then do another separation. char *token = strtok(str, ","); while(token){ char *current_string = malloc(sizeof(char) * strlen(token)); strcpy(current_string, token); char *tk = strtok(current_string, ":");…
Kagemand Andersen
  • 1,470
  • 2
  • 16
  • 30
4
votes
2 answers

Trying to understand the code in this implementation of strtok

I am very much a c newbie and I am learning by following CS107 videos from Standford (I am not a Student there). Links are below if anyone is interested Looking at the below implementation of strtok, I am not sure why the first if statement is…
Sam Hammamy
  • 10,819
  • 10
  • 56
  • 94
4
votes
6 answers

Trying to understand strtok

Consider the following snippet that uses strtok to split the string madddy. char* str = (char*) malloc(sizeof("Madddy")); strcpy(str,"Madddy"); char* tmp = strtok(str,"d"); std::cout<
Karthick
  • 2,844
  • 4
  • 34
  • 55