C 中的 strtok_r 和 strtok_s 有什么区别? [英] What's the difference between strtok_r and strtok_s in C?

查看:40
本文介绍了C 中的 strtok_r 和 strtok_s 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在需要能够在 Linux 和 Windows 中编译的 C 程序中使用此函数.起初我尝试使用 strtok_r,但是当我在 Windows 上编译时,它抱怨该函数不存在并说它会假设它是一个外部函数,但后来失败了.然后我使用 strtok_s 并编译它!然后我在 Linux 上尝试,但现在它抱怨有一个对'strtok_s'的未定义引用".

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'".

一个是windows唯一的函数,另一个是linux函数???我该怎么做才能让它在两者上都编译?

Is one a windows only function and the other a linux function??? What can I do to make it compile on both?

推荐答案

这两个函数对于解析字符串来说都是非常丑陋、不直观的习惯用法,并且通常无法以微妙的方式满足特定应用程序的要求.对于标准 C 中的普通 strtok 更是如此.只需将它们扔掉并编写您自己的代码来迭代 char 数组并根据需要将其分解.strchrstrspnstrcspn 可以帮助执行此操作,或者您可以从头开始处理数组.

Both of these functions are really ugly, unintuitive idioms for parsing strings, and usually fail to meet your particular application's requirements in subtle ways. Even moreso for the plain strtok in standard C. Just throw them out and write your own code to iterate over the char array and break it up as needed. strchr, strspn, and strcspn can be helpful in doing this or you can just work from scratch on the array.

这篇关于C 中的 strtok_r 和 strtok_s 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆