[Glib] Exemple de code

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

.

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

.

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.

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

[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 dochttp://library.gnome.org/devel/glib/unstable/glib-File-Utilities.html

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

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

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.

mon fichier existe avant … ou pas :mrgreen:
les 2 fonctionnent

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

hé hé, le retour aux sources :wink: