#include <thread>
#include <atomic>
#include <stdio.h>
#include <iostream>

using namespace std ;

struct my_point_deleter {  
    void operator()(FILE* f) const 
 {
     cout << "my_point_deleter called !!" << endl ;
     fclose(f) ;
 }
};

void func(FILE *f)
{
    cout << "func called " << endl ;
    fclose(f) ;
}

int main()
{  
    #ifdef TEST1
    unique_ptr<FILE, void(*) (FILE*)> f(fopen("hanoi.c", "r"),[](FILE* f){cout << "going closed" << endl; fclose(f);});
 #endif
    #ifdef TEST2
    unique_ptr<FILE, my_point_deleter> f(fopen("hanoi.c", "r")) ;
 #endif
    #ifdef TEST3
    unique_ptr<FILE, void(*) (FILE*)> f(fopen("hanoi.c", "r"),&func) ;
 #endif

 
 char aux[100];  
 while (!feof(f.get())) //f.get returns me the actual plain old pointer  
 {     
     fgets(aux, 100, f.get());     
  cout << aux;  
 }     //No need to invoke fclose() because the unique_ptr will take care of it.
}

 

創作者介紹
創作者 hedgezzz 的頭像
hedgezzz

hedgezzz的部落格

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