Function

GLib-2.0GLibfopenSince 2.6

  • A wrapper for the stdio fopen() function. The fopen() function opens a file and associates a new stream with it.

    Because file descriptors are specific to the C library on Windows, and a file descriptor is part of the FILE struct, the FILE* returned by this function makes sense only to functions in the same C library. Thus if the GLib-using code uses a different C library than GLib does, the FILE* returned by this function cannot be passed to C library functions like fprintf() or fread().

    See your C library manual for more details about fopen().

    As close() and fclose() are part of the C library, this implies that it is currently impossible to close a file if the application C library and the C library used by GLib are different. Convenience functions like g_file_set_contents_full() avoid this problem.

    Since GLib 2.86, the e option is supported in mode on all platforms. On Unix platforms it will set O_CLOEXEC on the opened file descriptor. On Windows platforms it will be converted to the N modifier. It is recommended to set e unconditionally, unless you know the returned file should be shared between this process and a new fork.

    Parameters

    • filename: string

      a pathname in the GLib file name encoding (UTF-8 on Windows)

    • mode: string

      a string describing the mode in which the file should be opened

    Returns any

    A FILE* if the file was successfully opened, or null if an error occurred

    2.6