2017-01-29 2 views
2

をファイル記述子ではないことが基本となるファイルディスクリプタを閉じずに、FILEラッパーを解放することが可能であった場合、私は疑問に思っ:C:近いFILE * -

void stream_function(int fd) { 
    FILE * stream = fdopen(fd, "r"); 
    // Read from stream with fread/fscanf 
    // then close the buffered stream object with the mythical 
    // xclose(stream) function, leaving the fd intact. 
    xclose(stream); 
} 


int main() { 
    int fd = open("filename" , flags); 
    stream_function(fd); 
    // Continue to use fd 
    close(fd); 
} 

答えて

0

それはありません。 dup2(fileno(f))を使用してファイルディスクリプタのコピーを保持することができますが、fclose(f)は本質的に、常にfileno(f)を閉じます。あなたはFILE *で始まることを得るためにfdopenを使用しているあなたのようなケースでは

は、しかし、あなたはfdopenに渡す前にFDをdup2することができます。これにより、元のfdがfclose時に閉じられるのを防ぎます。