gpt4 book ai didi

c++ - 为什么编译器在我定义了 _CRT_SECURE_NO_WARNINGS 之后仍然警告我不安全的 strtok?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:59:38 25 4
gpt4 key购买 nike

我正在使用适用于 Windows 桌面的 Visual Studio Express 2012。

我总是出错

Error C4996: 'strtok': This function or variable may be unsafe.
Consider using strtok_s instead.
To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
See online help for details.

当我尝试构建以下内容时:

#include "stdafx.h"
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char the_string[81], *p;
cout << "Input a string to parse: ";
cin.getline(the_string, 81);
p = strtok(the_string, ",");
while (p != NULL) {
cout << p << endl;
p = strtok(NULL, ",");
}
system("PAUSE");
return 0;
}

为什么即使我定义了 _CRT_SECURE_NO_WARNINGS 也会出现此错误,我该如何解决?

最佳答案

由于预编译头文件 (stdafx.h) 的内容,您的 #define 不起作用。样板文件看起来像这样:

#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>

导致问题的是最后两个#include,那些.h 文件本身已经#include string.h。因此,您的#define 为时已晚。

除了在编译器设置中定义宏之外,简单的解决方法是将#define 移动到您的 stdafx.h 文件中。修复:

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>

关于c++ - 为什么编译器在我定义了 _CRT_SECURE_NO_WARNINGS 之后仍然警告我不安全的 strtok?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21044177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com