# \[Glib\] Exemple de code

**URL:** <https://www.debian-fr.org/t/glib-exemple-de-code/14516>\
**Category:** Programmation\
**Created:** [Février 20, 2016, 5:47 UTC](https://www.debian-fr.org/t/glib-exemple-de-code/14516 "2016-02-20T17:47:48Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![getdr](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/g/ea5d25/32.png) [@getdr](https://www.debian-fr.org/u/getdr)\
**Post date:** [Février 20, 2016, 5:47 UTC](https://www.debian-fr.org/t/glib-exemple-de-code/14516/1 "2016-02-20T17:47:48Z")

</div>

Bonjour

Quelqu’un peut me donner un exemple de code pour créer un fichier et écrire une donnée dans ce fichier avec glib

Merci

---

<div class="post-metadata">

**Author:** ![tellmewhy](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/43a26b/32.png) [@tellmewhy](https://www.debian-fr.org/u/tellmewhy)\
**Post date:** [Février 20, 2016, 5:47 UTC](https://www.debian-fr.org/t/glib-exemple-de-code/14516/2 "2016-02-20T17:47:52Z")

</div>

.

---

<div class="post-metadata">

**Author:** ![getdr](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/g/ea5d25/32.png) [@getdr](https://www.debian-fr.org/u/getdr)\
**Post date:** [Février 20, 2016, 5:47 UTC](https://www.debian-fr.org/t/glib-exemple-de-code/14516/3 "2016-02-20T17:47:52Z")

</div>

Salut  
Merci, c’est exactement ce que je voulais faire !! MAIS sauf que je voudrais utiliser la librairie GLIB.

---

<div class="post-metadata">

**Author:** ![tellmewhy](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/43a26b/32.png) [@tellmewhy](https://www.debian-fr.org/u/tellmewhy)\
**Post date:** [Février 20, 2016, 5:47 UTC](https://www.debian-fr.org/t/glib-exemple-de-code/14516/4 "2016-02-20T17:47:55Z")

</div>

.

---

<div class="post-metadata">

**Author:** ![antalgeek1](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/a/a6a055/32.png) [@antalgeek1](https://www.debian-fr.org/u/antalgeek1)\
**Post date:** [Février 20, 2016, 5:47 UTC](https://www.debian-fr.org/t/glib-exemple-de-code/14516/5 "2016-02-20T17:47:56Z")

</div>

cherche un coup du côté de g\_file\_set\_contents et g\_file\_get\_contents :

[code]gboolean g\_file\_set\_contents (const gchar \*filename,  
const gchar \*contents,  
gssize length,  
GError \*\*error);

Writes all of contents to a file named filename, with good error checking. If a file called filename already exists it will be overwritten.

This write is atomic in the sense that it is first written to a temporary file which is then renamed to the final name. Notes:

```
* On Unix, if filename already exists hard links to filename will break. Also since the file is recreated, existing permissions, access control lists, metadata etc. may be lost. If filename is a symbolic link, the link itself will be replaced, not the linked file.
* On Windows renaming a file will not remove an existing file with the new name, so on Windows there is a race condition between the existing file being removed and the temporary file being renamed.
* On Windows there is no way to remove a file that is open to some process, or mapped into memory. Thus, this function will fail if filename already exists and is open.

```

If the call was sucessful, it returns TRUE. 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.

filename : name of a file to write contents to, in the GLib file name encoding  
contents : string to write to the file  
length : length of contents, or -1 if contents is a nul-terminated string  
error : return location for a GError, or NULL  
Returns : TRUE on success, FALSE if an error occurred[/code]

Tu peux installer devhelp il y a plein de doc à l’intérieur dedans.

---

<div class="post-metadata">

**Author:** ![mattotop](https://www.debian-fr.org/user_avatar/www.debian-fr.org/mattotop/32/3942_2.png) [@mattotop](https://www.debian-fr.org/u/mattotop)\
**Post date:** [Février 20, 2016, 5:47 UTC](https://www.debian-fr.org/t/glib-exemple-de-code/14516/6 "2016-02-20T17:47:56Z")

</div>

Je n’ai pas trop suivi le fil, mais si vous cherchez de la doc, pensez au man, par exemple chez moi ou je n’ai rien installé moi même de tout ça:

`roc@roc:~/devel/softs$ man -k glib
error (3) - Fonctions de compte-rendu d'erreurs de la glibc
error_at_line (3) - Fonctions de compte-rendu d'erreurs de la glibc
error_message_count (3) - Fonctions de compte-rendu d'erreurs de la glibc
error_on_per_line (3) - Fonctions de compte-rendu d'erreurs de la glibc
error_print_progname (3) - Fonctions de compte-rendu d'erreurs de la glibc
Glib (3pm) - Perl wrappers for the GLib utility and Object libraries
Glib API Reference Pod Index (3pm) [Glib::index] - API Reference Pod Index
glib-genmarshal (1) - C code marshaller generation utility for GLib closures
glib-gettextize (1) - gettext internationalization utility
glib-mkenums (1) - C language enum description generation utility
Glib::BookmarkFile (3pm) - Parser for bookmark files
Glib::Boxed (3pm) - Generic wrappers for C structures
Glib::CodeGen (3pm) - code generation utilities for Glib-based bindings.
Glib::devel (3pm) - Binding developer's overview of Glib's internals
Glib::Error (3pm) - Exception Objects based on GError
Glib::Flags (3pm) - Overloaded operators representing GLib flags
Glib::GenPod (3pm) - POD generation utilities for Glib-based modules
Glib::index (3pm) - API Reference Pod Index
Glib::KeyFile (3pm) - Parser for .ini-like files
Glib::Log (3pm) - A flexible logging mechanism
Glib::MainLoop (3pm) - An event source manager
Glib::MakeHelper (3pm) - Makefile.PL utilities for Glib-based extensions
Glib::Markup (3pm) - Wrapper for markup handling functions in GLib
Glib::Object (3pm) - Bindings for GObject
Glib::Object::Subclass (3pm) - register a perl class as a GObject class
Glib::Param::Boolean (3pm) - Wrapper for boolean parameters in GLib
Glib::Param::Double (3pm) - Wrapper for double parameters in GLib
Glib::Param::Enum (3pm) - Wrapper for enum parameters in GLib
Glib::Param::Flags (3pm) - Wrapper for flag parameters in GLib
Glib::Param::Int (3pm) - Paramspecs for integer types
Glib::Param::Int64 (3pm) - Wrapper for int64 parameters in GLib
Glib::Param::String (3pm) - Wrapper for string parameters in GLib
Glib::Param::UInt (3pm) - Wrapper for uint parameters in GLib
Glib::Param::UInt64 (3pm) - Wrapper for uint64 parameters in GLib
Glib::Param::Unichar (3pm) - Wrapper for unichar parameters in GLib
Glib::ParamSpec (3pm) - Wrapper to encapsulate metadate needed to specify parameters
Glib::ParseXSDoc (3pm) - Parse POD and XSub declarations from XS files.
Glib::Signal (3pm) - Object customization and general purpose notification
Glib::Type (3pm) - Utilities for dealing with the GLib Type system
Glib::Utils (3pm) - Miscellaneous utility functions
Glib::version (3pm) - Library Versioning Utilities
Glib::xsapi (3pm) - internal API reference for GPerl.
locale.glibc (1) - Get locale-specific information.
localedef.glibc (1) - compile locale definition files
`

---

<div class="post-metadata">

**Author:** ![getdr](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/g/ea5d25/32.png) [@getdr](https://www.debian-fr.org/u/getdr)\
**Post date:** [Février 20, 2016, 5:48 UTC](https://www.debian-fr.org/t/glib-exemple-de-code/14516/7 "2016-02-20T17:48:03Z")

</div>

[quote=“antalgeek”]cherche un coup du côté de g\_file\_set\_contents et g\_file\_get\_contents :

[code]gboolean g\_file\_set\_contents (const gchar \*filename,  
const gchar \*contents,  
gssize length,  
GError \*\*error);

Writes all of contents to a file named filename, with good error checking. If a file called filename already exists it will be overwritten.

This write is atomic in the sense that it is first written to a temporary file which is then renamed to the final name. Notes:

```
* On Unix, if filename already exists hard links to filename will break. Also since the file is recreated, existing permissions, access control lists, metadata etc. may be lost. If filename is a symbolic link, the link itself will be replaced, not the linked file.
* On Windows renaming a file will not remove an existing file with the new name, so on Windows there is a race condition between the existing file being removed and the temporary file being renamed.
* On Windows there is no way to remove a file that is open to some process, or mapped into memory. Thus, this function will fail if filename already exists and is open.

```

If the call was sucessful, it returns TRUE. 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.

filename : name of a file to write contents to, in the GLib file name encoding  
contents : string to write to the file  
length : length of contents, or -1 if contents is a nul-terminated string  
error : return location for a GError, or NULL  
Returns : TRUE on success, FALSE if an error occurred[/code]

Tu peux installer devhelp il y a plein de doc à l’intérieur dedans.[/quote]

Merci

en fait j’ai déjà utiliser cette fonction dans mon code, mais il m’indique erreur NULL.

en plus ce qui est bizzard, qaund j’utilise g\_creat(), il m’indique que cette fonction n’existe pas, alors qu’elle est référencé dans la doc[http://library.gnome.org/devel/glib/unstable/glib-File-Utilities.html](http://library.gnome.org/devel/glib/unstable/glib-File-Utilities.html)

---

<div class="post-metadata">

**Author:** ![antalgeek1](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/a/a6a055/32.png) [@antalgeek1](https://www.debian-fr.org/u/antalgeek1)\
**Post date:** [Février 20, 2016, 5:48 UTC](https://www.debian-fr.org/t/glib-exemple-de-code/14516/8 "2016-02-20T17:48:06Z")

</div>

voici comment j’utilise g\_file\_set\_contents en C, si cela peut t’aider  
les chaines de caractères sont toujours terminées par NULL

```auto
char *sTemp2;

// construction de la chaine à écrire dans le fichier
sTemp2 = g_strconcat ( "Salut", sToto, sTiti, NULL );
// liberer ce qui ne sert plus
g_free ( sToto );
g_free ( sTiti );

if ( !g_file_set_contents( "/home/bidule/monfichier", sTemp2, -1, NULL) )
{
  g_free ( sTemp2 );
  return FALSE;
}
```

je n’ai jamais eu trop de probleme avec les g\_file…  
si tu veux balance des bouts de ton code

---

<div class="post-metadata">

**Author:** ![getdr](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/g/ea5d25/32.png) [@getdr](https://www.debian-fr.org/u/getdr)\
**Post date:** [Février 20, 2016, 5:48 UTC](https://www.debian-fr.org/t/glib-exemple-de-code/14516/9 "2016-02-20T17:48:07Z")

</div>

lu

dans ton code ‘monfichier’ existe déjà dans ton répertoire.  
moi je veux le crée juste avant.  
g\_file\_set\_contents(variables…);

di style.

Je crée mon fichier avec je crois:  
g\_creat() ou g\_open()  
puis je l’ouvre et j’écris une donnée.

---

<div class="post-metadata">

**Author:** ![antalgeek1](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/a/a6a055/32.png) [@antalgeek1](https://www.debian-fr.org/u/antalgeek1)\
**Post date:** [Février 20, 2016, 5:48 UTC](https://www.debian-fr.org/t/glib-exemple-de-code/14516/10 "2016-02-20T17:48:08Z")

</div>

mon fichier existe avant … ou pas ![:mrgreen:](https://www.debian-fr.org/uploads/default/original/1X/cc83e9b583138e48ff45f96609046b06d06c7f9d.gif "Mr. Green")  
les 2 fonctionnent

---

<div class="post-metadata">

**Author:** ![getdr](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/g/ea5d25/32.png) [@getdr](https://www.debian-fr.org/u/getdr)\
**Post date:** [Février 20, 2016, 5:48 UTC](https://www.debian-fr.org/t/glib-exemple-de-code/14516/11 "2016-02-20T17:48:09Z")

</div>

ok  
merci je crois que je vais utiliser la bonne veille fonction fopen

---

<div class="post-metadata">

**Author:** ![antalgeek1](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/a/a6a055/32.png) [@antalgeek1](https://www.debian-fr.org/u/antalgeek1)\
**Post date:** [Février 20, 2016, 5:48 UTC](https://www.debian-fr.org/t/glib-exemple-de-code/14516/12 "2016-02-20T17:48:09Z")

</div>

hé hé, le retour aux sources 😉
