我只想使用GNOME glib函数来简单地编写和读取文件。我认为我的语法在调用函数时是错误的。我试图用g_fopen打开一个文件(“filenam.txt”,“w”);但它没有创建任何文件。我还使用了g_file_set_contents,我试图将我的Gstring保存到文件file.txt中,代码为
static void events_handler(const uint8_t *pdu, uint16_t len, gpointer user_data)
{
uint8_t *opdu;
uint16_t handle, i, olen;
size_t plen;
//GString *s;
const gchar *s;
gssize length;
length = 100;
handle = get_le16(&pdu[1]);
switch (pdu[0]) {
case ATT_OP_HANDLE_NOTIFY:
s = g_string_new(NULL);
//g_string_printf(s, "Movement data = 0x%04x value: ",handle);
g_file_set_contents("file.txt", s, 100, NULL);
break;
case ATT_OP_HANDLE_IND:
s = g_string_new(NULL);
g_string_printf(s, "Indication handle = 0x%04x value: ",handle);
break;
default:
error("Invalid opcode\n");
return;
}
for (i = 3; i < len; i++)
g_string_append_printf(s, "%02x ", pdu[i]);
rl_printf("%s\n", s->str);
g_string_free(s, TRUE);
if (pdu[0] == ATT_OP_HANDLE_NOTIFY)
return;
opdu = g_attrib_get_buffer(attrib, &plen);
olen = enc_confirmation(opdu, plen);
if (olen > 0)
g_attrib_send(attrib, 0, opdu, olen, NULL, NULL, NULL);
}
答案 0 :(得分:0)
您正在混淆GString*
和gchar*
。 g_string_*()
函数需要GString*
,g_file_set_contents()
期望gchar*
。如果您需要原始数据,请使用str
field。
另外,我建议在你的编译器上再发一些警告,因为如果你试图这样做,它确实应该在开发过程中抱怨。传递-Wall
应该可以解决问题......