Replace C-style library file handling with RAII approaches