g_try_new()

本文深入探讨了Glib库中的g_try_new、g_strdup和g_file_get_contents等核心字符串操作函数,包括参数说明、返回值及使用示例,帮助开发者熟练掌握Glib库的字符串处理能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://developer.gnome.org/glib/2.28/glib-String-Utility-Functions.html

g_try_new()

#define             g_try_new(struct_type, n_structs)

Attempts to allocate n_structs elements of type struct_type, and returns NULL on failure. Contrast with g_new(), which aborts the program on failure. The returned pointer is cast to a pointer to the given type. If n_structs is 0 it returns NULL.

struct_type :

the type of the elements to allocate

n_structs :

the number of elements to allocate

Returns :

a pointer to the allocated memory, cast to a pointer to struct_type


g_strdup ()

gchar *	               g_strdup                         (const gchar *str);

Duplicates a string. If str is NULL it returns NULL. The returned string should be freed with g_free() when no longer needed.

str :

the string to duplicate

Returns :

a newly-allocated copy of str

g_file_get_contents ()

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

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 setserror. 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 andlength is set to zero.

filename :

name of a file to read contents from, in the GLib file name encoding. [type filename]

contents :

location to store an allocated string, use g_free() to free the returned string. [out][array length=length][element-type guint8]

length :

location to store length in bytes of the contents, or NULL[allow-none]

error :

return location for a GError, or NULL

Returns :

TRUE on success, FALSE if an error occurred




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值