void funcclose(FILE* f)
{
    printf("file going to close \n") ;
    fclose(f) ;
}

 

unique_ptr<FILE, void (*)(FILE*)> f(fopen("test.txt", "r"),funcclose) ;

 

如果 test.txt  存在的話 ,  funcclose 在  fopen 產生的指標 (f) 結束後會被呼叫 ,

當 test.txt 不存在 ,  f = NULL , funcclose 不會被呼叫 !!

arrow
arrow
    全站熱搜

    hedgezzz 發表在 痞客邦 留言(0) 人氣()