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

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

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…
Heather Wilson
  • 153
  • 2
  • 4
  • 13
4
votes
2 answers

How can I do strtok()-type parsing in Python?

The title of How do I do what strtok() does in C, in Python? suggests it should answer my question but the specific strtok() behavior I'm looking for is breaking on any one of the characters in the delimiter string. That is, given: const char*…
Chris Nelson
  • 3,519
  • 7
  • 40
  • 51
3
votes
4 answers

Parse string into array based on spaces or "double quotes strings"

Im trying to take a user input string and parse is into an array called char *entire_line[100]; where each word is put at a different index of the array but if a part of the string is encapsulated by a quote, that should be put in a single index. So…
Airon Zagarella
  • 707
  • 2
  • 11
  • 17
3
votes
3 answers

Token string cuts off when inserting into 2d array in C

I am correctly tokenizing single words from a line of strings; however, inserting them into a 2d array cuts off parts of the token. I also have a problem with NULL, and the code results in a segfault. #include #include #include…
3
votes
1 answer

Why should strtok(line, "\n") **not** be used to strip the newline left by fgets()

fgets() is a safe function to read a line of input, but it stores the new line byte '\n' read from the file in the array if it fits. In many if not most cases, this new line must be removed before further processing the line contents. Several simple…
chqrlie
  • 131,814
  • 10
  • 121
  • 189
3
votes
2 answers

I misunderstand win32 (and maybe libc) strtok( )

In some CGI code, I need to encode rarely-occurring '&', '<', and '>' chars. In the encoding function, I want to get out right away if there are no such chars in the input string. So, at entry, I try to use strtok( ) to find that out: char…
Pete Wilson
  • 8,610
  • 6
  • 39
  • 51
3
votes
3 answers

what(): basic_string::_M_construct null not valid

I am making a program where I need to use a function which stores a tokens of a string in a vector. The function did not work properly so I tried the function on a smaller program. Of course, I used string tokenizer function. But it is not working…
Aman Kumar
  • 294
  • 8
  • 20
3
votes
4 answers

Using pointer to character as the argument of strtok

I try to split the string by using strtok function. But the program become failed if i use the pointer to character as the argument of this function. If i initialize the string as s2 or s3 the program works well. But if i use pointer to character as…
Hitokiri
  • 3,607
  • 1
  • 9
  • 29
3
votes
1 answer

strtok c multiple chars as one delimiter

Is it possible to use multiple chars as one delimiter? I would like a string as separator for another string. char * input = "inputvalue1SEPARATORSTRINGinputvalue2SEPARATORSTRINGinputvalue2"; char * output = malloc(sizeof(char*)); char * delim =…
it-person
  • 98
  • 1
  • 11
3
votes
2 answers

strtok doesn't return any value

I want to write a programm that converts a string with numbers ("1 2 3") into an integer array. But strtok() doesn't return a value. Why not? My console output is just empty. Edit: I get no error message. This is my code: #include #include…
kame
  • 20,848
  • 33
  • 104
  • 159
3
votes
1 answer

Using fgets in combination with strtok to transform input to tokens

I am reading input from different text files. These text files are integers that are either separated by a space, a new line, or a combination of spaces and new lines. I want to convert these strings to integers, and use these integers for a sorting…
3
votes
1 answer

Reading from an input file and storing words into an array

The end goal is to output a text file where repeating words are encoded as single digits instead. The current problem I'm having is reading words and storing them into an array. #include #include #include #define…
SolidSnackDrive
  • 213
  • 2
  • 10
3
votes
2 answers

How to restore string after using strtok()

I have a project in which I need to sort multiple lines of text based on the second, third, etc word in each line, not the first word. For example, this line is first but this line is second finally there is this line and you choose to sort by…
nhlyoung
  • 67
  • 2
  • 8
3
votes
6 answers

using strtol on a string literal causing segmentation fault

I have a string that I get by getline() (more precisely I use a loop and getline to read a file line by line) Let's say the line is 12|34| Then I use strtok() to cut it down by substr = strtok(line, "|"); and store them into a string array with a…
Kent Wong
  • 143
  • 8
3
votes
2 answers

Is strtok in C89?

Is the strtok function in standard C89?
Amandeep Grewal
  • 1,801
  • 3
  • 19
  • 30