strtok

关于用strtok_s处理过的CString会导致之前的CString之间的赋值被同化的问题的解决方案

老子叫甜甜 提交于 2019-11-29 13:36:43
问题现象: 实验一: 在MFC中,如果实现 CString a, b; a = "12/24"; b = a; char * next, *item; item = strtok_s((LPSTR)(LPCTSTR)a, "/", &next); 会发现这时a和b的值相同,都是12,而不是b是12/24,a是12。 然而通常情况下,b=a操作是值传递,之后对a做修改是不会影响到b的,这里却影响到了。 *********************** 实验二: CString a, b; a = "12/24"; b = a; a="12"; 这时候, b是12/24,a是12。一切正常。 为了使实验一a不影响到b,解决方法: ********************** 实验三: CString a, b; a = "12/24"; b = ""; b += a; char * next, *item; item = strtok_s((LPSTR)(LPCTSTR)a, "/", &next); 之后在对a重新赋值就不会对b产生影响了。 暂时还不知道为什么会产生这种变化,感觉好像a和b内存共享了!!! 来源: CSDN 作者: jiayinhaoran 链接: https://blog.csdn.net/jiayinhaoran/article/details/51657471

strtok strtok_s & strsep

ε祈祈猫儿з 提交于 2019-11-29 13:35:52
c 语言下的字符串分割函数: 一、strtok(): 原型:char *strtok(char s[], const char *delim); 分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串 strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字符串中包含的所有字符。当strtok()在参数s的字符串中发现参数delim中包涵的分割字符时,则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回指向被分割出片段的指针。 strtok函数会破坏被分解字符串的完整,调用前和调用后的s已经不一样了。 一个列子: #include <iostream> using namespace std; int main() { char sentence[]="192.168...9...14"; cout<<"sentence = "<<sentence<<"\nThe tokens are:\n"; //char *p; char *token=strtok(sentence,"."); while(token!=NULL){ cout<<token<<" "; token=strtok(NULL,"."); } cout<<endl; cout<<

strtok and strtok_s

喜夏-厌秋 提交于 2019-11-29 13:35:36
语言的运行库为了支持多线程特性,必须做出一些改进。一种改进的办法就是修改所有的线程不安全的函数的参数列表,改成某种线程安全的版本。比如MSVC的CRT就提供了线程安全版本的strtok()函数:strtok_s,它们的原型如下: char *strtok(char *strToken, const char *strDelimit ); char *strtok_s( char *strToken, const char *strDelimit, char**context); 改进后的strtok_s增加了一个参数,这个参数context是由调用者提供一个char*指针,strtok_s将每次调用后的字符串位置保存在这个指针中。而之前版本的strtok函数会将这个位置保存在一个函数内部的静态局部变量中,如果有多个线程同时调用这个函数,有可能出现冲突。与MSVCCRT类似,Glibc也提供了一个线程安全版本的strtok()叫做strtok_r()。 但是很多时候改变标准库函数的做法是不可行的。标准库之所以称之为“标准”,就是它具有一定的权威性和稳定性,不能随意更改。如果随意更改,那么所有遵循该标准的程序都需要重新进行修改,这个“标准”是不是值得遵循就有待商榷了。所以更好的做法是不改变任何标准库函数的原型,只是对标准库的实现进行一些改进,使得它能够在多线程的环境下也能够顺利运行

C语言字符串切割函数strtok_s()

让人想犯罪 __ 提交于 2019-11-29 13:34:43
strtok仅支持单线程,谨慎使用 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> void main() { //char s1[]="ls -l -a text"; char s2[]= "命令名: 命令选项: 命令选项: 命令参数:" , tmp[] = " " , *t; char s1[ 255 ]; char *result = NULL , *p = NULL ; int i, j, count = 0 ; printf( "请输入字符串s1...\n" ); fgets(s1, 100 , stdin); s1[strlen(s1) - 1 ] = '\0' ; //strtok不支持多线程,会引起信息混乱,使用strtok_s存储缓冲区数据 result = strtok_s(s1, tmp, &t); p = strtok(s2, tmp); while (result != NULL ) { printf( "%s %s\n" , p, result); result = strtok_s( NULL , tmp, &t); p = strtok( NULL , tmp); } } linux环境下编程请使用strtok_r函数替代strtok_s函数,否则会出错

strtok()和strtok_S()用法

时光毁灭记忆、已成空白 提交于 2019-11-29 13:33:29
strtok()头文件貌似在 《string.h》 不在stdlib.h string.h 是c库,string是C++ku <string.h> <string.h>是C版本的头文件,包含比如strcpy、strcat之类的字符串处理函数。 <cstring> 在C++标准化(1998年)过程中,为了兼容以前,标准化组织将所有这些文件都进行了新的定义,加入到了标准库中,加入后的文件名就新增了一个"c"前缀并且去掉了.h的后缀名,所以string.h头文件成了cstring头文件。但是其实现却是相同的或是兼容以前的,这就是<cstring>的来源,不要觉得又多了一个东西。相当于标准库组织给它盖了个章,说“你也是我的标准程序库的一份子了”。 <string> <string>是C++标准定义的头文件,它定义了一个string的字符串类,里面包含了string类的各种操作,如s.size(), s.erase(), s.insert()等。但<string>又包含了老的C版本的字符串操作如strcpy、strcat等,这就相当于,在<string>的文件中除了定义自己的string类之外,还加了一个#include<string.h>一句包含了C版本的字符串操作。 +++++++++++++++++++++ // #define _CRT_SECURE_NO_WARNINGS

strtok program crashing

◇◆丶佛笑我妖孽 提交于 2019-11-29 11:55:01
the program for strtok given on http://www.opengroup.org/onlinepubs/000095399/functions/strtok.html crashes everytime.. #include <string.h> ... char *token; char *line = "LINE TO BE SEPARATED"; char *search = " "; /* Token will point to "LINE". */ token = strtok(line, search); /* Token will point to "TO". */ token = strtok(NULL, search); If I use a char array for variable 'line', it works. i.e. char line[] = "LINE TO BE SEPARATED" works. Kindly explain. strtok modifies the input string line . char *line = "LINE TO BE SEPARATED"; In this case line points to the read-only memory. Hence, cannot

strtok() and empty fields

你说的曾经没有我的故事 提交于 2019-11-29 11:33:08
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? On linux there's strsep . The strsep() function was introduced as a replacement for strtok(), since the latter cannot handle empty fields. However, strtok() conforms to C89/C99 and hence is more portable. You can use strchr (for just one delimiter character) or strcspn (for a group of possible delimiters) to find the next delimiter, process the token, and then just step one character forward. Do this in a

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

☆樱花仙子☆ 提交于 2019-11-29 01:12:24
I'm trying to use this function in a C program that needs to be able to compile in Linux and Windows. At first I tried using strtok_r, but then when I compiled on windows, it complained about the function not existing and said it would assume it's an extern function, but then failed. I then used strtok_s and it compiled! Then I tried on Linux but now it's complaining that there is an "undefined reference to 'strtok_s'". Is one a windows only function and the other a linux function??? What can I do to make it compile on both? Both of these functions are really ugly, unintuitive idioms for

Segmentation Fault when using strtok_r

China☆狼群 提交于 2019-11-28 19:34:51
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++]); } } codaddict Try this: #include <stdio.h> #include <string.h> int main(void) { char hello[] = "Hello World, Let me live."; // make this a char array not a pointer to literal. char *rest; // to point to the rest of the string after token extraction. char *token; // to point to the actual token returned. char *ptr =

strtok_r for MinGW

。_饼干妹妹 提交于 2019-11-28 13:23:14
strtok_r is the reentrant variant of strtok. It is POSIX-conformant. However, it is missing from MinGW, and I'm trying to compile a program that is using it. Is there any way I could add a standard implementation of this function, perhaps to the project's own code, or to MinGW's standard library functions? Since there are some license questions about the code from another answer, here's one that's explicitly public domain: /* * public domain strtok_r() by Charlie Gordon * * from comp.lang.c 9/14/2007 * * http://groups.google.com/group/comp.lang.c/msg/2ab1ecbb86646684 * * (Declaration that it's