【问题标题】:Compiling codes with *_s in Visual C++ 6.0在 Visual C++ 6.0 中使用 *_s 编译代码
【发布时间】:2013-01-07 03:57:42
【问题描述】:

我正在尝试在 Visual C++ 6.0 中编译一些旧代码。缺少 DSW 文件,因此我将所有代码添加到新工作区中。

我有a.cpp如下

#include "vld.h"
#include "afx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "b.h"

...

void function_1(char* string1)
{
   char buf[2000];
   strcpy_s(&buf[0], 2000, string1);
   strcat_s(&buf[0], 2000, "_append");
   ...
}

a.cpp 中还使用了其他函数,例如 fopen_s, strncpy_s, strncat_s

b.h

#include <stdio.h>
#include <string.h>

char function_2(unsigned char * string2, int abc, int def)
{
   char buf2[200];
   sprintf_s(buf2, 200, "abcdef");
   strcat_s(buf2, 200, "ghijkl");
}

尽管已经有 stdio.hstring.h,但我仍然收到错误

'sprintf_s': undeclared identifier
'strcat_s': undeclared identifier
'strcpy_s': undeclared identifier
'fopen_s': undeclared identifier
'strncpy_s': undeclared identifier
'strncat_s': undeclared identifier

对于a.cppb.h

有没有我遗漏的设置?为什么会出现这些错误?

【问题讨论】:

  • 与其直接传递2002000,不如传递sizeof bufsizeof buf2。这意味着如果你改变数组的大小,所有的字符串函数都会自动对新的大小进行操作。这仅适用于 bufbuf2 是数组类型。例如,如果它们被声明为 char *,这将不起作用。

标签: string visual-c++ stdio


【解决方案1】:

Visual Studio 2005 增加了 _s 版本功能,所以你需要使用更现代的 Visual Studio 版本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    • 2019-07-31
    相关资源
    最近更新 更多