glib:读取文件 g_file_get_contents 用法
glib :File Utilities 读取文件内容
gboolean g_file_get_contents (const gchar *filename, gchar **contents, gsize *length,GError **error);
如果filename含utf8格式,参考g_filename_to_utf8用法
//此处有中文路径有可能乱码出错,原因还不清楚.完美中文支持应该用 GString,下面有示例 gchar * txt, *ph0="D:\\电子书\\test\\LICENSE.txt"; //中文路径按下面两行这样处理最保险, 要free GString *str_ph=g_string_new(ph0); ph0=str_ph->str; //如果路径中含utf8则必须如下处理, 如果全英文路径可以跳过 gchar* ph01 =g_locale_to_utf8( ph0 ,-1, NULL, NULL ,NULL); //此处为逆还原用法,备注 /* gchar* ph02 =g_locale_from_utf8( ph01 ,-1, NULL, NULL ,NULL); printf( ph02 ); */ g_file_get_contents(ph01,&txt,NULL,NULL); //实参txt用完要free printf( txt ); g_free(txt); g_string_free(str_ph,0);
浙公网安备 33010602011771号