# File Utilities

Various file-related classes and functions. More...

## Classes

class | Glib::FileError
| Exception class for file-related errors. More...
class | Glib::DirIterator
| The iterator type of Glib::Dir. More...
class | Glib::Dir
| Utility class representing an open directory. More...

## Functions

bool | Glib::file_test (const std::string& filename, FileTest test)
| Returns `true` if any of the tests in the bitfield test are true.
int | Glib::mkstemp (std::string& filename_template)
| Opens a temporary file.
int | Glib::file_open_tmp (std::string& name_used, const std::string& prefix)
| Opens a file for writing in the preferred directory for temporary files (as returned by Glib::get_tmp_dir()).
int | Glib::file_open_tmp (std::string& name_used)
| Opens a file for writing in the preferred directory for temporary files (as returned by Glib::get_tmp_dir()).
std::string | Glib::file_get_contents (const std::string& filename)
| Reads an entire file into a string, with good error checking.

## Detailed Description

Various file-related classes and functions.

## Function Documentation

std::string Glib::file_get_contents | ( | const std::string & | filename | ) |

Reads an entire file into a string, with good error checking.

Parameters:

  | filename | A file to read contents from.

Returns:
  The file contents.

Exceptions:

  | Glib::FileError |

int Glib::file_open_tmp | ( | std::string & | name_used | ) |

Opens a file for writing in the preferred directory for temporary files (as returned by Glib::get_tmp_dir()).

This function works like file_open_tmp(std::string&, const std::string&) but uses a default basename prefix.

Return values:

  | name_used | The actual name used.

Returns:
  A file handle (as from `open()`) to the file opened for reading and writing. The file is opened in binary mode on platforms where there is a difference. The file handle should be closed with `close()`.

Exceptions:

  | Glib::FileError |

int Glib::file_open_tmp | ( | std::string & | name_used,
|  | const std::string & | prefix |
| ) |  |  |

Opens a file for writing in the preferred directory for temporary files (as returned by Glib::get_tmp_dir()).

prefix should a basename template; it'll be suffixed by 6 characters in order to form a unique filename. No directory components are allowed.

The actual name used is returned in name_used.

Parameters:

  | prefix | Template for file name, basename only.

Return values:

  | name_used | The actual name used.

Returns:
  A file handle (as from `open()`) to the file opened for reading and writing. The file is opened in binary mode on platforms where there is a difference. The file handle should be closed with `close()`.

Exceptions:

  | Glib::FileError |

bool Glib::file_test | ( | const std::string & | filename,
|  | FileTest | test |
| ) |  |  |

Returns `true` if any of the tests in the bitfield test are true.

For example, `(Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_DIR)` will return `true` if the file exists; the check whether it's a directory doesn't matter since the existence test is true. With the current set of available tests, there's no point passing in more than one test at a time.

Apart from `Glib::FILE_TEST_IS_SYMLINK` all tests follow symbolic links, so for a symbolic link to a regular file file_test() will return `true` for both `Glib::FILE_TEST_IS_SYMLINK` and `Glib::FILE_TEST_IS_REGULAR`.

Note:
  For a dangling symbolic link file_test() will return `true` for `Glib::FILE_TEST_IS_SYMLINK` and `false` for all other flags.

Parameters:

  | filename | A filename to test.
  | test | Bitfield of Glib::FileTest flags.

Returns:
  Whether a test was true.

int Glib::mkstemp | ( | std::string & | filename_template | ) |

Opens a temporary file.

See the mkstemp() documentation on most UNIX-like systems. This is a portability wrapper, which simply calls mkstemp() on systems that have it, and implements it in GLib otherwise.

Parameters:

  | filename_template | A string that should match the rules for mkstemp(), i.e. end in `"XXXXXX"`. The `X` string will be modified to form the name of a file that didn't exist.

Returns:
  A file handle (as from open()) to the file opened for reading and writing. The file is opened in binary mode on platforms where there is a difference. The file handle should be closed with close(). In case of errors, `-1` is returned.