Function

GLibfile_get_contents

Declaration

gboolean
g_file_get_contents (
  const gchar* filename,
  gchar** contents,
  gsize* length,
  GError** error
)

Description

Reads an entire file into allocated memory, with good error checking.

If the call was successful, it returns TRUE and sets contents to the file contents and length to the length of the file contents in bytes. The string stored in contents will be nul-terminated, so for text files you can pass NULL for the length argument. If the call was not successful, it returns FALSE and sets error. The error domain is G_FILE_ERROR. Possible error codes are those in the GFileError enumeration. In the error case, contents is set to NULL and length is set to zero.

Parameters

filename

Type: const gchar*

Name of a file to read contents from, in the GLib file name encoding.

The data is owned by the caller of the function.
The value is a file system path, using the OS encoding.
contents

Type: An array of guint8

Location to store an allocated string, use g_free() to free the returned string.

The argument will be set by the function.
The length of the array is specified in the length argument.
The called function takes ownership of the data, and is responsible for freeing it.
length

Type: gsize*

Location to store length in bytes of the contents, or NULL.

The argument will be set by the function.
The argument can be set to NULL by the function.
error

Type: GError **

The return location for a recoverable error.

The argument can be NULL.
If the return location is not NULL, then you must initialize it to a NULL GError*.
The argument will left initialized to NULL by the function if there are no errors.
In case of error, the argument will be set to a newly allocated GError; the caller will take ownership of the data, and be responsible for freeing it.

Return value

Type: gboolean

TRUE on success, FALSE if an error occurred.