这些函数因为不是十分安全的,对于内存不够的时候处理并不完善,所以建议使用 更为安全的函数版本
比如_tgetenv_s();_tcscat_s(),_tsplitpath_s()等
_tgetenv_s()函数声明:
- errno_t getenv_s(
- size_t *pReturnValue,
- char* buffer,
- size_t numberOfElements,
- const char *varname
- );
- errno_t _wgetenv_s(
- size_t *pReturnValue,
- wchar_t *buffer,
- size_t numberOfElements,
- const wchar_t *varname
- );
- template <size_t size>
- errno_t getenv_s(
- size_t *pReturnValue,
- char (&buffer)[size],
- const char *varname
- ); // C++ only
- template <size_t size>
- errno_t _wgetenv_s(
- size_t *pReturnValue,
- wchar_t (&buffer)[size],
- const wchar_t *varname
- ); // C++ only
_tgetenv_s()用法如下:
- TCHAR *querystringvar;
- size_t requiredsize;
- _tgetenv_s(&requiredsize,NULL,0,_T("QUERY_STRING"));
- querystringvar = (TCHAR *)malloc(requiredsize * sizeof(TCHAR));
- if(!querystringvar)
- {
- fprintf(stdout, "Content-Type:text/html;charset=gbk;");
- fprintf(stdout, "/n/n");
- printf("获取QUERY_STRING时内存分配失败!/n");
- exit(1);
- }
- _tgetenv_s( &requiredsize, querystringvar, requiredsize, _T("QUERY_STRING") );
- CString strdata(querystringvar);
- free(querystringvar);
- querystringvar = NULL;
_setmode() ,如_setmode(_fileno(stdout),_O_BINARY);需要的头文件为
#include <io.h>//_setmode()需要
#include <fcntl.h>//_O_BINARY 需要
用法:
- #include <stdio.h>
- #include <fcntl.h>
- #include <io.h>
- int main( void )
- {
- int result;
- // Set "stdin" to have binary mode:
- result = _setmode( _fileno( stdin ), _O_BINARY );
- if( result == -1 )
- perror( "Cannot set mode" );
- else
- printf( "'stdin' successfully changed to binary mode/n" );
- }
对于 _tcscat_s() MSDN上如下声明:
- errno_t strcat_s(
- char *strDestination,
- size_t numberOfElements,
- const char *strSource
- );
- errno_t wcscat_s(
- wchar_t *strDestination,
- size_t numberOfElements,
- const wchar_t *strSource
- );
- errno_t _mbscat_s(
- unsigned char *strDestination,
- size_t numberOfElements,
- const unsigned char *strSource
- );
- template <size_t size>
- errno_t strcat_s(
- char (&strDestination)[size],
- const char *strSource
- ); // C++ only
- template <size_t size>
- errno_t wcscat_s(
- wchar_t (&strDestination)[size],
- const wchar_t *strSource
- ); // C++ only
- template <size_t size>
- errno_t _mbscat_s(
- unsigned char (&strDestination)[size],
- const unsigned char *strSource
- ); // C++ only
自己的例子:
- if(_tcscat_s(drivename,dirname) != 0)
- {
- fprintf(stdout, "Content-Type:text/html;charset=gbk;");
- fprintf(stdout, "/n/n");
- printf("瓦片库路径过长,程序将退出/n");
- exit(1);
- }
_tsplitpath_s() 声明如下:
- errno_t _splitpath_s(
- const char * path,
- char * drive,
- size_t driveSizeInCharacters,
- char * dir,
- size_t dirSizeInCharacters,
- char * fname,
- size_t nameSizeInCharacters,
- char * ext,
- size_t extSizeInBytes
- );
- errno_t _wsplitpath_s(
- const wchar_t * path,
- wchar_t * drive,
- size_t driveSizeInCharacters,
- wchar_t *dir,
- size_t dirSizeInCharacters,
- wchar_t * fname,
- size_t nameSizeInCharacters,
- wchar_t * ext,
- size_t extSizeInCharacters
- );
- template <size_t drivesize, size_t dirsize, size_t fnamesize, size_t extsize>
- errno_t _splitpath_s(
- const char *path,
- char (&drive)[drivesize],
- char (&dir)[dirsize],
- char (&fname)[fnamesize],
- char (&ext)[extsize]
- ); // C++ only
- template <size_t drivesize, size_t dirsize, size_t fnamesize, size_t extsize>
- errno_t _wsplitpath_s(
- const wchar_t *path,
- wchar_t (&drive)[size],
- wchar_t (&dir)[size],
- wchar_t (&fname)[size],
- wchar_t (&ext)[size]
- ); // C++ only
自己的例子
- if(_wsplitpath_s(apppath,drivename,dirname,filename,extname) != 0)
- //
- fprintf(stdout, "Content-Type:text/html;charset=gbk;");
- fprintf(stdout, "/n/n");
- printf("瓦片库路径过长,程序将退出/n");
- exit(1);
从上面的例子可以看出,对于这些函数,若是指针传递,则比原先不安全的版本多一个大小限制,而对于已经申请好的字符串引用传递,则可以和以前一样用(仅限C++,C里面没有引用),基本若是成功,则返回值为0,有误为其他返回值。
更详细的见MSDN