mirror of
				https://gitlab.gnome.org/GNOME/glib.git
				synced 2025-10-31 16:32:18 +01:00 
			
		
		
		
	Use the current g_file_get_contents() as example.
This commit is contained in:
		| @@ -39,14 +39,17 @@ This is why most functions in GLib and GTK+ do not use the #GError facility. | |||||||
| Functions that can fail take a return location for a #GError as their last argument.  | Functions that can fail take a return location for a #GError as their last argument.  | ||||||
| For example: | For example: | ||||||
| <informalexample><programlisting> | <informalexample><programlisting> | ||||||
| gchar* g_file_get_contents (const gchar *filename, GError **error); | gboolean g_file_get_contents (const gchar *filename,  | ||||||
|  | 	                      gchar      **contents, | ||||||
|  |                               gsize       *length, | ||||||
|  |                               GError     **error); | ||||||
| </programlisting></informalexample> | </programlisting></informalexample> | ||||||
| If you pass a non-%NULL value for the <literal>error</literal> argument, it should  | If you pass a non-%NULL value for the <literal>error</literal> argument, it should  | ||||||
| point to a location where an error can be placed. For example: | point to a location where an error can be placed. For example: | ||||||
| <informalexample><programlisting> | <informalexample><programlisting> | ||||||
| gchar *contents; | gchar *contents; | ||||||
| GError *err = NULL; | GError *err = NULL; | ||||||
| contents = g_file_get_contents ("foo.txt", &err); | g_file_get_contents ("foo.txt", &contents, NULL, &err); | ||||||
| g_assert ((contents == NULL && err != NULL) || (contents != NULL && err == NULL)); | g_assert ((contents == NULL && err != NULL) || (contents != NULL && err == NULL)); | ||||||
| if (err != NULL) | if (err != NULL) | ||||||
|   { |   { | ||||||
| @@ -63,18 +66,16 @@ else | |||||||
| </programlisting></informalexample> | </programlisting></informalexample> | ||||||
| Note that <literal>err != NULL</literal> in this example is a | Note that <literal>err != NULL</literal> in this example is a | ||||||
| <emphasis>reliable</emphasis> indicator of whether | <emphasis>reliable</emphasis> indicator of whether | ||||||
| g_file_get_contents() failed. Also, g_file_get_contents() uses the | g_file_get_contents() failed. Additionally, g_file_get_contents() returns | ||||||
| convention that a %NULL return value means an error occurred (but not | a boolean which indicates whether it was successful. | ||||||
| all functions use this convention). |  | ||||||
| </para> | </para> | ||||||
|  |  | ||||||
| <para> | <para> | ||||||
| Because g_file_get_contents() returns %NULL on failure, if you are only | Because g_file_get_contents() returns %FALSE on failure, if you are only | ||||||
| interested in whether it failed and don't need to display an error message, you | interested in whether it failed and don't need to display an error message, you | ||||||
| can pass %NULL for the <literal>error</literal> argument: | can pass %NULL for the <literal>error</literal> argument: | ||||||
| <informalexample><programlisting> | <informalexample><programlisting> | ||||||
| contents = g_file_get_contents ("foo.txt", NULL); /* ignore errors */ | if (g_file_get_contents ("foo.txt", &contents, NULL, NULL)) /* ignore errors */ | ||||||
| if (contents != NULL) |  | ||||||
|   /* no error occurred */ ; |   /* no error occurred */ ; | ||||||
| else | else | ||||||
|   /* error */ ; |   /* error */ ; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user