使用glib库函数,能读出文件内容,但用g_key_file_load_from_data填结构体出错
我主要参考的是网上的这篇文章:http://www.diybl.com/course/6_system/linux/Linuxjs/2008625/128344.html
使用了把key对应的data写进去的 write_item 和 读key对应的data simple_get_conf_item两个方法。读出文件没有问题,但是在写入文件用write_item 方法时,用它的g_key_file_load_from_data 方法总是报错,这个方法是把内存中的内容填到一个结构体中去。
我在前面一句的printf("%s():data = %s\n", __FUNCTION__, file_data)中,看到要改写的内容都打印出来了,但这句却执行错误。不明白原因,请大家赐教。
以下是主要功能代码:
************
//get the old DB IP from conf/ss.conf
old_DB_address=simple_get_conf_item(Z_SS_V4_CONF_DIR,Z_CONFIG_DEF,Z_M_NAME_DBI, "MySqlHost");
g_debug("old database IP is %s\n",old_DB_address.c_str());
//if new DB'ip equal to old,then set myself to master,nor set to slave;
pathconfig = "d:/etc";
if (old_DB_address.compare(new_DB_address) == 0)
{
//this computer would set to master status;
new_DB_address = "";
}
//get hostname and
if( (ret = gethostname(hostname,128))==0)
{
sprintf(hostname_relay_bin, "rm -f /var/lib/mysql/%s-relay-bin.*",hostname);
sprintf(hostname_bin_index, "rm -f /var/lib/mysql/%s-relay-bin.index",hostname);
g_debug("the cmd1 is %s!\n",hostname_relay_bin);
g_debug("the cmd2 is %s! \n",hostname_bin_index);
}
//set the my.conf status;
ret= write_item(pathconfig,"my.txt", "mysqld", "master-host", (char * )new_DB_address.c_str());
if(ret==1)
{
g_debug("write_item error \n");
return -1;
}
*****************