_tgetenv(),_tcscat() ,setmode(),_tsplitpath()等函数的 was declared deprecated 警告

来源:互联网 发布:青蛙少年失踪事件知乎 编辑:程序博客网 时间:2024/05/17 04:29

这些函数因为不是十分安全的,对于内存不够的时候处理并不完善,所以建议使用 更为安全的函数版本

比如_tgetenv_s();_tcscat_s(),_tsplitpath_s()等

 

_tgetenv_s()函数声明:

[cpp] view plaincopy
  1. errno_t getenv_s(   
  2.    size_t *pReturnValue,  
  3.    char* buffer,  
  4.    size_t numberOfElements,  
  5.    const char *varname   
  6. );  
  7. errno_t _wgetenv_s(   
  8.    size_t *pReturnValue,  
  9.    wchar_t *buffer,  
  10.    size_t numberOfElements,  
  11.    const wchar_t *varname   
  12. );  
  13. template <size_t size>  
  14. errno_t getenv_s(   
  15.    size_t *pReturnValue,  
  16.    char (&buffer)[size],  
  17.    const char *varname   
  18. ); // C++ only  
  19. template <size_t size>  
  20. errno_t _wgetenv_s(   
  21.    size_t *pReturnValue,  
  22.    wchar_t (&buffer)[size],  
  23.    const wchar_t *varname   
  24. ); // C++ only  

_tgetenv_s()用法如下:

 

 

[cpp] view plaincopy
  1. TCHAR *querystringvar;  
  2.     size_t requiredsize;  
  3.   
  4.     _tgetenv_s(&requiredsize,NULL,0,_T("QUERY_STRING"));  
  5.   
  6.     querystringvar = (TCHAR *)malloc(requiredsize * sizeof(TCHAR));  
  7.     if(!querystringvar)  
  8.     {  
  9.         fprintf(stdout, "Content-Type:text/html;charset=gbk;");  
  10.         fprintf(stdout, "/n/n");  
  11.         printf("获取QUERY_STRING时内存分配失败!/n");  
  12.         exit(1);  
  13.     }  
  14.     _tgetenv_s( &requiredsize, querystringvar, requiredsize, _T("QUERY_STRING") );  
  15.   
  16.     CString strdata(querystringvar);  
  17.   
  18.     free(querystringvar);  
  19.     querystringvar = NULL;  

 

_setmode() ,如_setmode(_fileno(stdout),_O_BINARY);需要的头文件为

 #include  <io.h>//_setmode()需要

  #include <fcntl.h>//_O_BINARY 需要

用法:

 

[cpp] view plaincopy
  1. #include <stdio.h>  
  2. #include <fcntl.h>  
  3. #include <io.h>  
  4.   
  5. int main( void )  
  6. {  
  7.    int result;  
  8.   
  9.    // Set "stdin" to have binary mode:  
  10.    result = _setmode( _fileno( stdin ), _O_BINARY );  
  11.    if( result == -1 )  
  12.       perror( "Cannot set mode" );  
  13.    else  
  14.       printf( "'stdin' successfully changed to binary mode/n" );  
  15. }  

 

对于 _tcscat_s() MSDN上如下声明:

 

[c-sharp] view plaincopy
  1. errno_t strcat_s(  
  2.    char *strDestination,  
  3.    size_t numberOfElements,  
  4.    const char *strSource   
  5. );  
  6. errno_t wcscat_s(  
  7.    wchar_t *strDestination,  
  8.    size_t numberOfElements,  
  9.    const wchar_t *strSource   
  10. );  
  11. errno_t _mbscat_s(  
  12.    unsigned char *strDestination,  
  13.    size_t numberOfElements,  
  14.    const unsigned char *strSource   
  15. );  
  16. template <size_t size>  
  17. errno_t strcat_s(  
  18.    char (&strDestination)[size],  
  19.    const char *strSource   
  20. ); // C++ only  
  21. template <size_t size>  
  22. errno_t wcscat_s(  
  23.    wchar_t (&strDestination)[size],  
  24.    const wchar_t *strSource   
  25. ); // C++ only  
  26. template <size_t size>  
  27. errno_t _mbscat_s(  
  28.    unsigned char (&strDestination)[size],  
  29.    const unsigned char *strSource   
  30. ); // C++ only  

 

自己的例子:

 

[cpp] view plaincopy
  1.    if(_tcscat_s(drivename,dirname) != 0)  
  2. {  
  3.     fprintf(stdout, "Content-Type:text/html;charset=gbk;");  
  4.     fprintf(stdout, "/n/n");  
  5.     printf("瓦片库路径过长,程序将退出/n");  
  6.     exit(1);  
  7. }  

 

 

_tsplitpath_s() 声明如下:

[cpp] view plaincopy
  1. errno_t _splitpath_s(  
  2.    const char * path,  
  3.    char * drive,  
  4.    size_t driveSizeInCharacters,  
  5.    char * dir,  
  6.    size_t dirSizeInCharacters,  
  7.    char * fname,  
  8.    size_t nameSizeInCharacters,  
  9.    char * ext,   
  10.    size_t extSizeInBytes  
  11. );  
  12. errno_t _wsplitpath_s(  
  13.    const wchar_t * path,  
  14.    wchar_t * drive,  
  15.    size_t driveSizeInCharacters,  
  16.    wchar_t *dir,  
  17.    size_t dirSizeInCharacters,  
  18.    wchar_t * fname,  
  19.    size_t nameSizeInCharacters,  
  20.    wchar_t * ext,  
  21.    size_t extSizeInCharacters  
  22. );  
  23. template <size_t drivesize, size_t dirsize, size_t fnamesize, size_t extsize>  
  24. errno_t _splitpath_s(  
  25.    const char *path,  
  26.    char (&drive)[drivesize],  
  27.    char (&dir)[dirsize],  
  28.    char (&fname)[fnamesize],  
  29.    char (&ext)[extsize]  
  30. ); // C++ only  
  31. template <size_t drivesize, size_t dirsize, size_t fnamesize, size_t extsize>  
  32. errno_t _wsplitpath_s(  
  33.    const wchar_t *path,  
  34.    wchar_t (&drive)[size],  
  35.    wchar_t (&dir)[size],  
  36.    wchar_t (&fname)[size],  
  37.    wchar_t (&ext)[size]  
  38. ); // C++ only  

 

自己的例子

[cpp] view plaincopy
  1.   if(_wsplitpath_s(apppath,drivename,dirname,filename,extname) != 0)  
  2.   
  3. //  
  4. fprintf(stdout, "Content-Type:text/html;charset=gbk;");  
  5. fprintf(stdout, "/n/n");  
  6. printf("瓦片库路径过长,程序将退出/n");  
  7. exit(1);  

 

从上面的例子可以看出,对于这些函数,若是指针传递,则比原先不安全的版本多一个大小限制,而对于已经申请好的字符串引用传递,则可以和以前一样用(仅限C++,C里面没有引用),基本若是成功,则返回值为0,有误为其他返回值。

 

更详细的见MSDN


转帖:http://blog.csdn.net/shiwei0124/article/details/4923689