尝试使用 g_file_set_contents 写入文件
Trying to write to file using g_file_set_contents
我只想使用 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);
}
您将 GString*
和 gchar*
混为一谈。 g_string_*()
函数需要 GString*
,g_file_set_contents()
需要 gchar*
。如果您想要原始数据,请使用 str
field.
此外,我建议在您的编译器上打开更多警告,因为如果您尝试这样做,它确实应该在开发过程中发出警告。通过 -Wall
应该可以解决问题……
我只想使用 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);
}
您将 GString*
和 gchar*
混为一谈。 g_string_*()
函数需要 GString*
,g_file_set_contents()
需要 gchar*
。如果您想要原始数据,请使用 str
field.
此外,我建议在您的编译器上打开更多警告,因为如果您尝试这样做,它确实应该在开发过程中发出警告。通过 -Wall
应该可以解决问题……