strtok

Implementing Split function in C

混江龙づ霸主 提交于 2019-12-11 06:23:11
问题 I'm trying to write a C function that is similar to split in Java. When I do what I do at main, instead of add function, it works perfectly. But I couldn't understand why it does not work with add function. #include <stdio.h> #include <string.h> char *words[50] = { NULL }; void add(const char *word) { static int i = 0; words[i] = word; i++; } int main( void ) { char string[65]; char *tokenPtr; fgets(string, 65, stdin); tokenPtr = strtok( string, " " ); add(tokenPtr); int i = 0; while (

Read a line from text file and place data separated by spaces inside struct

帅比萌擦擦* 提交于 2019-12-11 06:21:03
问题 I've to write a function in C which takes as input a filepointer and returns a structure like this: typedef struct product{ char * code_product; char * name; char * code_piece; only_time_t enter; only_time_t exit; }product_t; This struct uses another struct like this: typedef struct only_time{ int hour; int minute; int second; }only_time_t; In order to read from the file I use the getline() function and the I use the strtok() function to create tokens. Here is the function I use to read from

C: strtok overriding previous array values?

不想你离开。 提交于 2019-12-11 04:12:52
问题 card * load_rolodex(FILE *read_file) { int array_index = 0; char line [LINE_MAX]; card *card_array = malloc(sizeof(card) * upper_bound); while (fgets(line, LINE_MAX, read_file)!= NULL) { card_array[array_index].last_name = strtok(line, " "); card_array[array_index].first_name = strtok(NULL, " "); card_array[array_index].phone_number = strtok(NULL, " "); size++; array_index++; } return card_array; } I am trying to save each token to values in a struct array. I'm reading from a file with the

get the last token of a string in C

不羁的心 提交于 2019-12-10 23:45:34
问题 what I want to do is given an input string, which I will not know it's size or the number of tokens, be able to print it's last token. e.x.: char* s = "some/very/big/string"; char* token; const char delimiter[2] = "/"; token = strtok(s, delimiter); while (token != NULL) { printf("%s\n", token); token = strtok(NULL, delimiter); } return token; and i want my return to be string but I what I get is (null). Any workarounds? I've searched the web and can't seem to find an answer to this. At least

Tokenizing multiple strings simultaneously

大城市里の小女人 提交于 2019-12-10 17:35:06
问题 Say I have three c-style strings, char buf_1[1024] , char buf_2[1024] , and char buf_3[1024] . I want to tokenize them, and do things with the first token from all three, then do the same with the second token from all three, etc. Obviously, I could call strtok and loop through them from the beginning each time I want a new token. Or alternatively, pre-process all the tokens, stick them into three arrays and go from there, but I'd like a cleaner solution, if there is one. 回答1: It sounds like

Parallel to PHP's “explode” in C: Split char* into char* using delimiter [duplicate]

时光怂恿深爱的人放手 提交于 2019-12-10 11:54:07
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Split string with delimiters in C I'm searching a good way to "explode" a char* into other char* using a delimiter. My delimiter will be # 回答1: You can use strtok like CrazyCasta said but his/hers code is wrong. char *tok; char *src = malloc(strlen(srcStr) + 1); memcpy(src, srcStr); tok = strtok(src, "#"); if(tok == NULL) { printf("no tokens found"); free(src); return ???; } printf("%s ; ", tok); while((tok =

How to use strtok in C properly so there is no memory leak?

我们两清 提交于 2019-12-10 01:50:23
问题 I am somewhat confused by what happens when you call strtok on a char pointer in C. I know that it modifies the contents of the string, so if I call strtok on a variable named 'line', its content will change. Assume I follow the bellow approach: void function myFunc(char* line) { // get a pointer to the original memory block char* garbageLine = line; // Do some work // Call strtok on 'line' multiple times until it returns NULL // Do more work free(garbageLine); } Further assume that 'line' is

Cannot concatenate strtok's output variable. strcat and strtok

筅森魡賤 提交于 2019-12-08 21:18:33
I’ve spent hours on this program and have put several hours online searching for alternatives to my methods and have been plagued with crashes and errors all evening… I have a few things I'd like to achieve with this code. First I’ll explain my problems, then I’ll post the code and finally I’ll explain my need for the program. The program outputs just the single words and the concatenate function does nothing. This seems like it should be simple enough to fix... My first problem is that I cannot seem to get the concatenate function to work, I used the generic strcat function which didn't work

Double split in C

回眸只為那壹抹淺笑 提交于 2019-12-08 19:41:58
问题 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 problems with separating further by , to get first and second number of each set. This is my current code: result = strtok(result, ";"); while(result != NULL ) { printf("%s\n", result); result = strtok(NULL, ";"); } It outputs me: 1,12 7,19 6,4 8,19 2

PHP Token replaces html entities

早过忘川 提交于 2019-12-08 19:30:35
I want to make certain words/strings like links if found in the text. I have a piece of code from php.bet which does that, but it also removes the beginning and end of tags from <a href="http://www.domain.com/index.php" title="Home">go to homepage</a> . Can you help solve this? Here's the piece of code: <?php $str_in = '<p>Hi there worm! You have a disease!</p><a href="http://www.domain.com/index.php" title="Home">go to homepage</a>'; $replaces= array( 'worm' => 'http://www.domain.com/index.php/worm.html', 'disease' => 'http://www.domain.com/index.php/disease.html' ); function addLinks($str_in