strtok

Split String in C to recognize consecutive tabs

Deadly 提交于 2021-02-17 04:32:04
问题 I have a file that has certain fields separated by tabs. There will always be 17 tabs but there order can vary, such as.. 75104\tDallas\t85\t34.46\t45.64 75205\tHouston\t\t37.34\t87.32 93434\t\t\t1.23\t3.32 When I use strtok in the following fashion while (fgets(buf, sizeof(buf), fp) != NULL) { tok = strtok(buf,"\t"); while(tok != NULL) { printf("%s->",tok); tok = strtok(NULL,"\t"); } } I get all the tokens, but double tabs \t\t or more are ignored. However, I need to know when a field is

Split String in C to recognize consecutive tabs

巧了我就是萌 提交于 2021-02-17 04:31:12
问题 I have a file that has certain fields separated by tabs. There will always be 17 tabs but there order can vary, such as.. 75104\tDallas\t85\t34.46\t45.64 75205\tHouston\t\t37.34\t87.32 93434\t\t\t1.23\t3.32 When I use strtok in the following fashion while (fgets(buf, sizeof(buf), fp) != NULL) { tok = strtok(buf,"\t"); while(tok != NULL) { printf("%s->",tok); tok = strtok(NULL,"\t"); } } I get all the tokens, but double tabs \t\t or more are ignored. However, I need to know when a field is

Minix: undefined reference to 'strtok'?

荒凉一梦 提交于 2021-02-10 07:54:09
问题 I am trying to add a new system call at: /usr/src/servers/pm/exec.c I write a very simple method in exec.c : void parseBlack(char * value){ char * ptr = strtok(values, ";"); } However, when I try to compile it there is an error: In function parseBlac, undefined reference to "strtok". And I have added #include <string.h> It is weird. I checked minix api. It has this method: /minix/include/string.h(http://code.metager.de/source/xref/minix/include/string.h) Here is a screen shot: 回答1: Servers in

Using pointer to character as the argument of strtok

僤鯓⒐⒋嵵緔 提交于 2021-02-08 05:28:15
问题 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 s1 the program get Segmentation fault (core dumped) . char *s1 = "1A 2B 3C 4D"; char s2[] = "1A 2B 3C 4D"; char s3[20] = "1A 2B 3C 4D"; The problem is the other functions, printf and strlen work without failure, but only strtok get error. The

What's the difference between strtok and strtok_r in C?

有些话、适合烂在心里 提交于 2021-01-21 00:49:28
问题 What's the difference between strtok and strtok_r in C and when are we supposed to use which? 回答1: strtok is equivalent to (and often defined as): char *strtok(char *str, const char *delim) { static char *save; return strtok_r(str, delim, &save); } in general, you should use strtok_r directly rather than strtok , unless you need to make your code portable to pre-POSIX-2001 systems that only support strtok 回答2: The _r versions of functions are reentrant: you can call them from multiple threads

Why is different result using strtok_r in Mac & Ubuntu

≡放荡痞女 提交于 2020-12-15 19:46:50
问题 I'm practice c Lang strtok_r(). Here is my code. #include<stdio.h> #include<string.h> #include<unistd.h> int main(int argc, char** argv){ char* ptr = NULL; char* next[2] = {0}; char delimiter[4]; int now = 1; strcpy(delimiter, " \t"); // check argc number if(argc != 3){ printf("usage : ./test {string} {string}\n"); return -1; } ptr = strtok_r(argv[1], delimiter, &next[1]); printf("ptr : %s, next : %s\n", ptr, next[1]); ptr = strtok_r(argv[2], delimiter, &next[2]); while((ptr = strtok_r(NULL,

Why is different result using strtok_r in Mac & Ubuntu

时光怂恿深爱的人放手 提交于 2020-12-15 19:35:24
问题 I'm practice c Lang strtok_r(). Here is my code. #include<stdio.h> #include<string.h> #include<unistd.h> int main(int argc, char** argv){ char* ptr = NULL; char* next[2] = {0}; char delimiter[4]; int now = 1; strcpy(delimiter, " \t"); // check argc number if(argc != 3){ printf("usage : ./test {string} {string}\n"); return -1; } ptr = strtok_r(argv[1], delimiter, &next[1]); printf("ptr : %s, next : %s\n", ptr, next[1]); ptr = strtok_r(argv[2], delimiter, &next[2]); while((ptr = strtok_r(NULL,

Why Null character does not adds up to the end of string when scanf'd?

浪子不回头ぞ 提交于 2020-05-28 11:55:07
问题 --start of snip-- char name[15]; ... printf("Enter employee name \n"); scanf("%s",name); printf("strlen %d \n", strlen(name)); --end of snip -- Output: Enter employee name Veronica 8 why is it not adding null character to the end !? am i missing anything? Please someone explain. Edited: Was reading line from opened file using fgets and used strtok(line,"\t ") to get the tokens from the line. --snip-- char * chk; char line[100]; char temp_name[15]; while(fgets(line, sizeof line, filep)) { chk