【发布时间】: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.h 和 string.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.cpp 和b.h。
有没有我遗漏的设置?为什么会出现这些错误?
【问题讨论】:
-
与其直接传递
200或2000,不如传递sizeof buf和sizeof buf2。这意味着如果你改变数组的大小,所有的字符串函数都会自动对新的大小进行操作。这仅适用于buf和buf2是数组类型。例如,如果它们被声明为char *,这将不起作用。
标签: string visual-c++ stdio