GLib – 2.0: File Utilities
Description: Reference for GLib-2.0: File Utilities

#### File Utilities

Do not use these APIs unless you are porting a POSIX application to Windows. A more high-level file access API is provided as GIO; see the documentation for `GFile`.

## POSIX File Wrappers

There is a group of functions which wrap the common POSIX functions dealing with filenames:

- `g_access()`
- `g_chdir()`
- `g_chmod()`
- `g_close()`
- `g_creat()`
- `g_fopen()`,
- `g_freopen()`
- `g_fsync()`
- `g_lstat()`
- `g_mkdir()`
- `g_open()`
- `g_remove()`
- `g_rename()`
- `g_rmdir()`
- `g_stat()`
- `g_unlink()`
- `g_utime()`

The point of these wrappers is to make it possible to handle file names with any Unicode characters in them on Windows without having to use `#ifdef`s and the wide character API in the application code.

On some Unix systems, these APIs may be defined as identical to their POSIX counterparts. For this reason, you must check for and include the necessary header files (such as `fcntl.h`) before using functions like `g_creat()`. You must also define the relevant feature test macros.

The pathname argument should be in the GLib file name encoding. On POSIX this is the actual on-disk encoding which might correspond to the locale settings of the process (or the `G_FILENAME_ENCODING` environment variable), or not.

On Windows the GLib file name encoding is UTF-8. Note that the Microsoft C library does not use UTF-8, but has separate APIs for current system code page and wide characters (UTF-16). The GLib wrappers call the wide character API if present (on modern Windows systems), otherwise convert to/from the system code page.

## POSIX Directory Wrappers

Another group of functions allows to open and read directories in the GLib file name encoding:

- `g_dir_open()`
- `g_dir_read_name()`
- `g_dir_rewind()`
- `g_dir_close()`

## Error Handling

- `g_file_error_from_errno()`

## Setting/Getting File Contents

- `g_file_get_contents()`
- `g_file_set_contents()`
- `g_file_set_contents_full()`

## File Tests

- `g_file_test()`
- `g_file_read_link()`

## Temporary File Handling

- `g_mkdtemp()`
- `g_mkdtemp_full()`
- `g_mkstemp()`
- `g_mkstemp_full()`
- `g_file_open_tmp()`
- `g_dir_make_tmp()`

## Building and Manipulating Paths

- `g_build_path()`
- `g_build_pathv()`
- `g_build_filename()`
- `g_build_filenamev()`
- `g_build_filename_valist()`
- `G_IS_DIR_SEPARATOR()`
- `g_path_is_absolute()`
- `g_path_skip_root()`
- `g_get_current_dir()`
- `g_path_get_basename()`
- `g_path_get_dirname()`
- `g_canonicalize_filename()`

## Creating Directories

- `g_mkdir_with_parents()`

## Deprecated API

- `g_basename()`