strtok

Converting Char * to Uppercase in C

纵饮孤独 提交于 2020-01-23 01:22:29
问题 I'm trying to convert a char * to uppercase in c, but the function toupper() doesn't work here. I'm trying to get the name of the the value of temp, the name being anything before the colon, in this case it's "Test", and then I want to capitalize the name fully. void func(char * temp) { // where temp is a char * containing the string "Test:Case1" char * name; name = strtok(temp,":"); //convert it to uppercase name = toupper(name); //error here } I'm getting the error that the function toupper

Reading the file with comma delimiter using fgets() and strtok()

不羁岁月 提交于 2020-01-15 23:37:25
问题 I have a text file with three fields separated by comma. Example of the content of my text file: 12345, true programming newbie, BS ME To load the file into the program, i used the below code.... my problem is that sometimes the code works and sometimes it doesn't (no error message appears the program just closes itself and doesn't continue). i also observed the text file is blank (nothing is written) it automatically closes itself and doesn't continue. Your help would be highly appreciated.

C++ strtok - multiple use with more data buffers

馋奶兔 提交于 2020-01-11 11:42:31
问题 I have little issue with using strtok() function. I am parsing two files. Firts I load file 1 into buffer . This file constains name of the second file I need to load. Both files are read line after line. My code looks like this: char second_file_name[128] = { "" }; char * line = strtok( buffer, "\n" ); while( line != NULL ) { if ( line[0] = 'f' ) { sscanf( line, "%*s %s", &second_file_name ); LoadSecondFile( second_file_name ); } // processing other lines, not relevant for question line =

字符串分割函数strtok(线程不安全),线程安全函数strtok_r

不想你离开。 提交于 2020-01-10 10:03:11
strtok_r函数---字符串分割函数 函数原型: char *strtok_r(char *str, const char *delim, char **saveptr); 参数: str:被分割的字符串,若str为NULL,则被分割的字符串为*saveptr delim:依据此字符串分割str saveptr:分割后剩余部分的字符串 返回值: 遇到第一个delim时,分割出的字符串,若没有遇到delim,则范围NULL 例程: int main(int argc,char* argv[]) { char str[1024] = "this is a test!"; char *token; char *saveptr; token = strtok_r(str, " ", &saveptr); printf("token:%s\n",token); printf("saveptr:%s\n\n\n",saveptr); token = strtok_r(saveptr, " ", &saveptr); printf("token:%s\n",token); printf("saveptr:%s\n",saveptr); return 0; } 结果: 3.strtok和strtok_r的源代码 这两个函数的实现,由众多的版本。我strtok_r来自于GNU C Library

strtok segmentation fault

大憨熊 提交于 2020-01-08 14:26:41
问题 I am trying to understand why the following snippet of code is giving a segmentation fault: void tokenize(char* line) { char* cmd = strtok(line," "); while (cmd != NULL) { printf ("%s\n",cmd); cmd = strtok(NULL, " "); } } int main(void) { tokenize("this is a test"); } I know that strtok() does not actually tokenize on string literals, but in this case, line points directly to the string "this is a test" which is internally an array of char . Is there any of tokenizing line without copying it

C fgets strtok and atoi to read a line in C

五迷三道 提交于 2020-01-06 06:40:46
问题 #include <stdio.h> #include <string.h> int main(void) { char string[10000],*token; int garden[100],i=0; fgets(string,10000,stdin); token = strtok(string," "); while(strcmp(token,"\n") != 0){ garden[i] = atoi(token); i++; token = strtok(NULL," "); } return 0; } What is wrong with this code ? Why can't I read space separated integers from a line ? 回答1: Change while(strcmp(token,"\n") != 0){ to while(token != NULL){ 回答2: Your code only works when there's a space after the final number. Otherwise

C fgets strtok and atoi to read a line in C

好久不见. 提交于 2020-01-06 06:40:23
问题 #include <stdio.h> #include <string.h> int main(void) { char string[10000],*token; int garden[100],i=0; fgets(string,10000,stdin); token = strtok(string," "); while(strcmp(token,"\n") != 0){ garden[i] = atoi(token); i++; token = strtok(NULL," "); } return 0; } What is wrong with this code ? Why can't I read space separated integers from a line ? 回答1: Change while(strcmp(token,"\n") != 0){ to while(token != NULL){ 回答2: Your code only works when there's a space after the final number. Otherwise

Use strtok read csv file

匆匆过客 提交于 2020-01-03 06:40:12
问题 I am trying to use strtok in C to read csv file, and store the contents into array of struct Game. My code is shown below: FILE *fp; int i = 0; if((fp=fopen("Games.csv","r"))==NULL) { printf("Can't open file.\n"); exit(1); } rewind(fp); char buff[1024]; fgets(buff,1024,fp); char* delimiter = ","; while(fgets(buff, 1024, (FILE*)fp)!=NULL && i<5){ Game[i].ProductID= strtok(buff, ","); Game[i].ProductName = strtok(NULL, delimiter); Game[i].Publisher = strtok(NULL, delimiter); Game[i].Genre =

PHP Token replaces html entities

喜夏-厌秋 提交于 2020-01-03 02:49:04
问题 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

Is there a way to count tokens in C?

谁说我不能喝 提交于 2020-01-01 02:35:09
问题 I'm using strtok to split a string into tokens. Does anyone know any function which actually counts the number of tokens? I have a command string and I need to split it and pass the arguments to execve() . Thanks! Edit execve takes arguments as char** , so I need to allocate an array of pointers. I don't know how many to allocate without knowing how many tokens are there. 回答1: One approach would be to simply use strtok with a counter. However, that will modify the original string. Another