什么功能可以代替“strcpy”代替? [英] what function can replace in stead of "strcpy" ?

查看:872
本文介绍了什么功能可以代替“strcpy”代替?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在visual studio 2012中得到这样的错误:

错误C4996:'strcpy':此函数或变量可能不安全。请考虑使用strcpy_s。要禁用弃用,请使用_CRT_SECURE_NO_WARNINGS



也可以在char * cPtr = strtok(cStr,);











get error like this in visual studio 2012:
error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS

also get this in "char *cPtr = strtok (cStr, " ");"





void parser(string line, int fileNo, class HashTable & HT, frequencyList FL[])
{
        // Variables declarations
        //char * cStr, * cPtr;
        string tempString;



        char* cStr=new char[line.length()+1];
        strcpy(cStr ,line.c_str());

        /*
        // Define cStr = the size of line + 1
        // Copy the string line to char * cStr
        cStr = new char [line.size() + 1];
        strcpy (cStr, line.c_str());
        */
        // Parese cStr by " "
        char *cPtr = strtok (cStr, " ");
        while (cPtr != NULL)
        {
                if (strcmp(cPtr, "</TEXT>") != 0 && *cPtr != '.')
                {
                        tempString = string(cPtr);

                        // Call removeCommaBehind function to remove , behind word
                        removeCommaBehind(tempString);

                       
                     
                        if(tempString.compare(",") != 0)
                        if(tempString.compare(".") != 0)
                        if(tempString.compare("=") != 0)
                        {
                                // Call countFrequency() function
                                countFrequency(tempString, FL);
                        }
                }
                cPtr = strtok(NULL, "()/ ");
        }
        delete[] cStr;
}

推荐答案

在这里你可以找到你的问题的解决方案:



Stackoverflow
Here you can find the solution for your question:

Stackoverflow


错误消息本身提示修复:使用 strcpy_s [ ^ ]和 strtok_s [ ^ ]函数。
The error message itself suggest the fix: use the strcpy_s[^] and strtok_s[^] functions.


错误消息本身已经说明了两种可能的解决方案。你也可以谷歌错误代码或直接跳转到错误帮助使用F1来查找这篇文章 [ ^ ]



简而言之:告诉自己MicroSofts推出新功能的原因并弃用旧功能,然后决定是否要使用它们,并相应地更改代码,或使用 _CRT_SECURE_NO_WARNINGS



如果您选择后一个选项,请不要将定义放入您的代码中,将其添加到您的预编译设置。它是非标准的C ++,所以没有必要用它来污染你的代码......
The error message itself already states two possible solutions. You could also google the error code or directly jump to the error help using F1 to find this article[^]

In short: inform yourself about MicroSofts reasons to introduce the new functions and deprecate the old ones, then decide whether you want to use them or not, and change your code accordingly, or use _CRT_SECURE_NO_WARNINGS.

If you go for the latter option, don't put the definition into your code, add it to your precompile settings. It's non-standard C++, so no point polluting your code with it...


这篇关于什么功能可以代替“strcpy”代替?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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