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 不會被呼叫 !!
全站熱搜
留言列表