use open instead of fopen to avoid memory leak