文件的HANDLE转化为FILE*

文件的HANDLE转化为FILE*

#include  < iostream >
#include 
< fcntl.h >
#include 
< io.h >
#include 
< afxwin.h >
using   namespace  std;

void  Test() { 
    HANDLE   hFile   
=    CreateFile(  " c:\\test.dat  "
        GENERIC_READ   
|    GENERIC_WRITE,    0 ,   NULL,   
        OPEN_ALWAYS,   FILE_ATTRIBUTE_NORMAL,   NULL); 

    
char    szText[]    =     " Hello   world!\n  "
    DWORD   dwWritten; 
    WriteFile(hFile,   szText,   strlen(szText),   
& dwWritten,   NULL); 

    FILE
*    pFile    =    NULL; 
    
int    nHandle    =    _open_osfhandle(( long )hFile, _O_TEXT | _O_APPEND); 
    
if    (nHandle    !=     - 1 ) {
        pFile   
=    _fdopen(nHandle,    " wt  " ); 
    }

    
if (pFile) { 
        
int    n    =    fputs(  " write   by   FILE*!  " ,   pFile);
        fflush(pFile);
// 立即写入文件
    } 
    CloseHandle(hFile); 
}

int  main()
{
    Test();
    
return   0 ;
}

你可能感兴趣的:(文件的HANDLE转化为FILE*)