Questions tagged [strsep]

Questions involving the use of the BSD strsep() function.

strsep() is a function originating in BSD Unix which operates in a similar manner to the standard C strtok() function. The main difference is that this function may return empty tokens, whereas strtok() returns only non-empty tokens.

56 questions
34
votes
3 answers

What are the differences between strtok and strsep in C

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.
mizuki
  • 343
  • 1
  • 3
  • 4
7
votes
3 answers

Parsing a string in C with strsep (alternative methods)

I want to parse a string, and I use strsep function: #include #include #include int main() { char str[] = "Marco:Q:2F7PKC"; char *token1, *token2, *token3; char *r = malloc(30); strcpy(r, str); token1 =…
Kyrol
  • 3,475
  • 7
  • 34
  • 46
6
votes
3 answers

strsep() usage and its alternative

#include #include int main() { char *slogan = "together{kaliya} [namak]"; char *slow_gun = strdup(slogan); char *token = strsep(&slow_gun, "{"); printf ("\n slow_gun: %s\n token: %s\n", slow_gun, token); return 0; } when I…
hari
  • 9,439
  • 27
  • 76
  • 110
3
votes
1 answer

Segfault after strsep only when compiling with clang 10

I am writing a parser (for NMEA sentences) which splits a string on commas using strsep. When compiled with clang (Apple LLVM version 10.0.1), the code segfaults when splitting a string which has an even number of tokens. When compiled with clang…
3
votes
2 answers

Why doesn't strsep() work with pointers to the stack?

It appears that there is a pointer compatibility problem using the function strsep to find the first word of a string. So far I always thought that char *s and char s[] are completely interchangeable. But it appears they are not. My program using an…
3
votes
2 answers

getline() / strsep() combination causes segmentation fault

I'm getting a segmentation fault when running the code below. It should basically read a .csv file with over 3M lines and do other stuff afterwards (not relevant to the problem), but after 207746 iterations it returns a segmentation fault. If I…
Arduino
  • 373
  • 1
  • 3
  • 21
2
votes
2 answers

Splitting String in C procudes Segmentation fault (core dumped)

I'm trying to read from the keyboard something like a command e.g. start game,info1,info2 and I want to split and save those two strings, one having to do with what kind of command the user,types in and the other having information about the…
Gofoboso
  • 21
  • 2
2
votes
2 answers

Getting the error "undefined reference to 'strsep'" with Clang and MinGW

Here is the relevant code: #define _GNU_SOURCE #define BUFFER_SIZE 1024 #include #include #include int main(void) { while (1) { char* buffer; size_t size = 32; size_t line; line =…
BrennanWal
  • 63
  • 1
  • 5
2
votes
4 answers

Use fscanf to read in variable numbers of integer

I have over 100,000 csv files in the below…
Chi-fung LAM
  • 195
  • 2
  • 11
2
votes
0 answers

What are the implications and usage guidelines for strsep's obseletion of strtok?

The (BSD) man pages for strtok state in the first line under description This interface is obsoleted by strsep(3). Does that mean that strtok should on the whole not be used? It does not provide any usage guidelines about strtok's being obsolete…
Eli Sadoff
  • 7,173
  • 6
  • 33
  • 61
2
votes
1 answer

Storing pointers to strsep() return values

When I use strsep() to iterate through the tokens of a string, is it safe for me to store pointers to those tokens, and refer to them later? Example: char str[] = "some word tokens"; char *sameStr = str; char *token, *old1, *old2; token =…
Pat Flegit
  • 183
  • 1
  • 2
  • 11
2
votes
2 answers

strsep() function - more characters in delimiter

I am having problems using the strsep() function in C. I want to split a string into two parts. The string contains info about the currently playing song, in ARTIST - TITLE format, so artist and title are separated by one space, one dash and again…
XploD
  • 367
  • 3
  • 15
2
votes
1 answer

why strsep segment fault

#include #include #include int main() { char *buf = "2012/9/8"; char sep[] = "/"; char *token; // char *bp = strdup(buf); char *bp = buf; while ((token = strsep(&bp,sep))) { printf("tok = `%s'\n",…
youhong
  • 33
  • 1
  • 4
1
vote
1 answer

Can't find out what's wrong with the strsep() function in C

I'm new to C and started making a program. Here's how it looks like: #include #include #include int main() { char input[10000]; printf(" >>> "); gets(input); printf("\n\n"); if (strlen(input) >…
1
vote
1 answer

Having issues splitting strings when using the strsep function

I'm very new to C programming and was trying to split a string through it's delimiters using the strsep function. When executing the code below i get this output: Hostname ( teste-13-f8-04teste-13-fd-80) Hostname (teste-13-fd-80) Hostname…
Termanly
  • 23
  • 3
1
2 3 4