GLib Reference Manual | |||
---|---|---|---|
<<< Previous Page | Home | Up | Next Page >>> |
#include <glib.h> enum GFileError; #define G_FILE_ERROR enum GFileTest; GFileError g_file_error_from_errno (gint err_no); gboolean g_file_get_contents (const gchar *filename, gchar **contents, guint *length, GError **error); gboolean g_file_test (const gchar *filename, GFileTest test); int g_mkstemp (char *tmpl); int g_file_open_tmp (const char *tmpl, char **name_used, GError **error); |
typedef enum { G_FILE_ERROR_EXIST, G_FILE_ERROR_ISDIR, G_FILE_ERROR_ACCES, G_FILE_ERROR_NAMETOOLONG, G_FILE_ERROR_NOENT, G_FILE_ERROR_NOTDIR, G_FILE_ERROR_NXIO, G_FILE_ERROR_NODEV, G_FILE_ERROR_ROFS, G_FILE_ERROR_TXTBSY, G_FILE_ERROR_FAULT, G_FILE_ERROR_LOOP, G_FILE_ERROR_NOSPC, G_FILE_ERROR_NOMEM, G_FILE_ERROR_MFILE, G_FILE_ERROR_NFILE, G_FILE_ERROR_BADF, G_FILE_ERROR_INVAL, G_FILE_ERROR_PIPE, G_FILE_ERROR_AGAIN, G_FILE_ERROR_INTR, G_FILE_ERROR_IO, G_FILE_ERROR_PERM, G_FILE_ERROR_FAILED } GFileError; |
typedef enum { G_FILE_TEST_IS_REGULAR = 1 << 0, G_FILE_TEST_IS_SYMLINK = 1 << 1, G_FILE_TEST_IS_DIR = 1 << 2, G_FILE_TEST_IS_EXECUTABLE = 1 << 3, G_FILE_TEST_EXISTS = 1 << 4 } GFileTest; |
gboolean g_file_get_contents (const gchar *filename, gchar **contents, guint *length, GError **error); |
Reads an entire file into allocated memory, with good error checking. If error is set, FALSE is returned, and contents is set to NULL. If TRUE is returned, error will not be set, and contents will be set to the file contents. The string stored in contents will be nul-terminated, so for text files you can pass NULL for the length argument. The error domain is G_FILE_ERROR. Possible error codes are those in the GFileError enumeration.
FIXME currently crashes if the file is too big to fit in memory; should probably use g_try_malloc() when we have that function.
filename : | a file to read contents from |
contents : | location to store an allocated string |
length : | location to store length in bytes of the contents |
error : | return location for a GError |
Returns : | TRUE on success, FALSE if error is set |
gboolean g_file_test (const gchar *filename, GFileTest test); |
Returns TRUE if any of the tests in the bitfield test are TRUE. For example, (G_FILE_TEST_EXISTS | G_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.
filename : | a filename to test |
test : | bitfield of GFileTest flags |
Returns : | whether a test was TRUE |
int g_mkstemp (char *tmpl); |
Open a temporary file. See "man mkstemp" 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.
The parameter is a string that should match the rules for mktemp, i.e. end in "XXXXXX". The X string will be modified to form the name of a file that didn't exist.
tmpl : | template filename |
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. |
int g_file_open_tmp (const char *tmpl, char **name_used, GError **error); |
Opens a file for writing in the preferred directory for temporary files (as returned by g_get_tmp_dir()).
tmpl should be a string ending with six 'X' characters, as the parameter to g_mkstemp() (or mkstemp()). However, unlike these functions, the template should only be a basename, no directory components are allowed. If template is NULL, a default template is used.
Note that in contrast to g_mkstemp() (and mkstemp()) tmpl is not modified, and might thus be a read-only literal string.
The actual name used is returned in name_used if non-NULL. This string should be freed with g_free when not needed any longer.
tmpl : | Template for file name, as in g_mkstemp, basename only |
name_used : | location to store actual name used |
error : | return location for a GError |
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 and error will be set. |