您的位置:首页 > 编程语言 > Java开发

资源加载 - 相对路径获取JAVA配置文件

2013-06-30 15:49 671 查看
以下使用strncpy 但不安全
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <windows.h>

int main (int argc, char *argv[])
{
char *p = "hello who you are ? ";
char *dest;
char s[20];
int valLen;

dest = (char *) malloc (sizeof (char) * 1000);

//if we use strncpy_s we will get assert
//for the size is not enough
//we can just change s[20] to s[21]
/*
strncpy_s(s, _countof(s), p, strlen(p));
printf("%s\n", s);
*/

// Here we use strncpy and get null termination
strncpy (dest, p, (valLen = strlen(p)));
dest[valLen] = '\0';
printf ("%s\n", dest);

//system ("pause");
return 0;
}

以下用strncpy 我们认为它更安全
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <windows.h>

int main (int argc, char *argv[])
{
char *p = "hello who you are ? ";
char *dest;
char s[20];
int valLen;

dest = (char *) malloc (sizeof (char) * 1000);

//if we use strncpy_s we will get assert
//for the size is not enough
//we can just change s[20] to s[21]

strncpy_s(s, _countof(s), p, strlen(p));
printf("%s\n", s);

// Here we use strncpy and get null termination
/*
strncpy (dest, p, (valLen = strlen(p)));
dest[valLen] = '\0';
printf ("%s\n", dest);
*/
//system ("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: