strtok

strtok and replace a substring in C++

If I have a string "12 23 34 56" What's the easiest way to change it to "\x12 \x23 \x34 \x56"? ...

Using strtok with a string argument (instead of char*)?

I have a string that I would like to tokenize. But the strtok() function requires my string to be a char*. How can I do this quickly? token = strtok(str.c_str(), " "); fails because it turns it into a const char*, not a char* ...

developed a strtok alternative.

Hello, 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. */ int i = 0; while(*source) { *dest++ = *source++; if(*source ...

Is there an equivalent to Perls' split() function in C?

I'm trying to split real numbers in a C program using the decimal point as the delimter such that such that say, 1234.56 yields (int) whole_num = 1234 (int) fraction = 56 Any ideas how I can go about doing this? Its been a loooong while since I mucked around with C, see? :) ...

strtok and function call

I have this extremely strange behavior coming : In the below code: If I comment the call to MyLogger then everything works fine that is I get the sTempNr tokenized and 4 tokens are printed . But if I uncomment the call to MyLogger for logging then only the iteration takes place once and in other testing class with similar code as below ...

C - strtok and strcmp

I am having a bit of trouble using strtok with strcmp. //Handles the header sent by the browser char* handleHeader(char *header){ //Method given by browser (will only take GET, POST, and HEAD) char *method,*path, *httpVer; method = (char*)malloc(strlen(header)+1); strcpy(method,header); method = ...

strtok and memory leaks

hello, I wrote a simple url parser using strtok(). here's the code #include <stdio.h> #include <stdlib.h> typedef struct { char *protocol; char *host; int port; char *path; } aUrl; void parse_url(char *url, aUrl *ret) { printf("Parsing %s\n", url); char *tmp = (char *)_strdup(url); //char *protocol, *host...

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 necessary, it'd be a great help. Why do i need to explicitly advance the token rather tha...

strtok and execlp in a mini-shell

Hi fellows! I'm writing a mini-shell to get more familiar with Unix process management in C. It's reading stuff from commandline and passes these arguments via execlp to the system. # include <stdio.h> # include <stdlib.h> # include <unistd.h> #define MAXSIZE 100 char prompt[MAXSIZE]; int main(void){ pid_t pid; printf("> ...

Why do I get an EXC_BAD_ACCESS here and how can I make it work?

I'm trying to make my code be able to separate a file into a customer database (it's delimited by many spaces and not tabs). I try to use strtok, but I get an EXC_BAD_ACCESS error. Here is my main.cpp code: #include <iostream> #include <fstream> #include <string> #include <sstream> #include "Cust.h" using namespace std; int main (int a...

how do I parse a ',' separated char string using c?

What is the easiest way of parsing a comma separated list, where there can be zero elements between each token. The cstring could look like 1, 3, 4, 5, 6, 7, 8, .... But could also look like , , , , , , , , , ... I've tried something like: char *original = "1, 3, 4, 5, 6, 7, 8, ...." char *tok = strtok(original," ,") while(tok!=NU...

Help! strcmp is lying to me when fed strtok results.

Hey Folks, strcmp, when fed the results of strtok, in the following code seems to be blatantly lying to me. int fSize; char * buffer=NULL; char * jobToken = "job"; char * nextToken=NULL; job * curJob=NULL; struct node * head=NULL; struct node * parseList(FILE* file){ fseek(file,0,SEEK_END); fSize=ftell(file); buffer = (cha...

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 single space as the delimiter. And I would like a malloc'ed array of malloc'ed strings char...

ANSI C: How to split a string by newline and get a random line

Hi, I'm a new to C and got stuck with subj. I can split string with strtok but I don't know how to get a random token. Thanks. ...

Segmentation Fault when using strtok_r

Can anyone explain why I am getting segmentation fault in the following example? #include <stdio.h> #include <string.h> int main(void) { char *hello = "Hello World, Let me live."; char *tokens[50]; strtok_r(hello, " ,", tokens); int i = 0; while(i < 5) { printf("%s\n", tokens[i++]); } } ...

c++ strtok problem

i'm trying to create a map of word==>drow, like polindrom... the problem is at the final level at "strtok"... first i split it, then in subsequent call when doing strtok(NULL," "); it works ok. the problem is when i add the second string "poly_buffer"... seems it works on it.... #include "stdafx.h" #include <iostream> #include <cstdio> ...

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 <stdio.h> #include <stdlib.h> #include <string.h> #define STRMAX 128 int main() { /* Declarations */ FILE* fptr; int iCntr = 0; char sLine[STRMAX]; char* ...

Why isn't C++ strtok() working for me?

The program is supposed to receive an input through cin, tokenize it, and then output each one to show me that it worked properly. It did not. The program compiles with no errors, and takes an input, but fails to output anything. What am I doing wrong? int main(int argc, char* argv[]) { string input_line; while(std::cin >> input_...

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? ...

Use strtol or strtok to parse a string in C?

The string input would be > bc <address1> <address2> length I can break the string into tokens using strtok but not sure how to take each separate token and for example convert address1 and address 2 to hex. void tokenize() { char str[] ="bc 0xFFFF0 0xFFFFF 30"; char *tkn; char *tkn2; tkn = strtok (str," "); while (tkn != NUL...