Thursday, July 14, 2011

c++ secure library - from visual studio 2005

_s ==> secure library


  1. sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l 
    • MSDN: http://msdn.microsoft.com/en-us/library/ce3zzk1k(VS.80).aspx
    • Korean blogs: http://blog.naver.com/tomatoalive?Redirect=Log&logNo=40077924809
  2. fopen_s, _wfopen_s 
    • MSDN: http://msdn.microsoft.com/en-us/library/z5hh6ee9(v=VS.80).aspx
    • Korean blogs: http://warmz.tistory.com/87
  3. Examples from MSDN
    • // crt_sprintf_s.c
      // This program uses sprintf_s to format various
      // data and place them in the string named buffer.
      //
      
      #include <stdio.h>
      
      int main( void )
      {
         char  buffer[200], s[] = "computer", c = 'l';
         int   i = 35, j;
         float fp = 1.7320534f;
      
         // Format and print various data: 
         j  = sprintf_s( buffer, 200,     "   String:    %s\n", s );
         j += sprintf_s( buffer + j, 200 - j, "   Character: %c\n", c );
         j += sprintf_s( buffer + j, 200 - j, "   Integer:   %d\n", i );
         j += sprintf_s( buffer + j, 200 - j, "   Real:      %f\n", fp );
      
         printf_s( "Output:\n%s\ncharacter count = %d\n", buffer, j );
      }
      #include <stdio.h>
      
      FILE *stream, *stream2;
      
      int main( void )
      {
         int numclosed;
         errno_t err;
      
         // Open for read (will fail if file "crt_fopen_s.c" does not exist)
         if( (err  = fopen_s( &stream, "crt_fopen_s.c", "r" )) !=0 )
            printf( "The file 'crt_fopen_s.c' was not opened\n" );
         else
            printf( "The file 'crt_fopen_s.c' was opened\n" );
      
         // Open for write 
         if( (err = fopen_s( &stream2, "data2", "w+" )) != 0 )
            printf( "The file 'data2' was not opened\n" );
         else
            printf( "The file 'data2' was opened\n" );
      
         // Close stream if it is not NULL 
         if( stream)
         {
            if ( fclose( stream ) )
            {
               printf( "The file 'crt_fopen_s.c' was not closed\n" );
            }
         }
      
         // All other files are closed:
         numclosed = _fcloseall( );
         printf( "Number of files closed by _fcloseall: %u\n", numclosed );
      }

No comments:

Post a Comment