使用巧舌如簧写在文件中的数据 [英] Data written in file using glib

查看:270
本文介绍了使用巧舌如簧写在文件中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个code写入℃的号码长度的字符;长度GT; 在一个文件中,使用g_file_set_contents。当我打开文件,我看到似乎是ASCII像@&放一些奇怪的字符; @@。我认为该数据可能会被写在ASCII格式,从二进制转换,所以我用一个函数来从ASCII转换为二进制。我仍然没有得到执行后的任何决议。结果
这里是code

I have a code that writes a number of characters of length <length> in a file, using g_file_set_contents. When I open the file, I see some weird characters that seem to be ASCII like @&@@. I assumed that the data might be written in ASCII format, converting from binary, so I used a function to convert from ASCII to binary. I still did not get any resolution after execution.
Here is the code

#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char** argv) 
{

 FILE *file = g_fopen("Multicore","w");
 gchar *contents = 00001111;
 gchar **contents1 = NULL;
 GError *err = NULL;
 g_file_set_contents ("Multicore", &contents, 8, &err);
 g_assert ((contents == NULL && err != NULL) || (contents != NULL && err == NULL));
 if (err != NULL)
  {
    g_assert (contents == NULL);
    fprintf (stderr, "Unable to read file: %s\n", err->message);
    g_error_free (err);
  } 
 else
  {
    g_assert (contents != NULL);
  }
  int p = g_ascii_digit_value(contents);
  if (g_ascii_isdigit (contents))
    return contents - '0';
  return -1;
  g_printf(" The output is %c \n", contents);
  return 0;
}

我正确地得到输出为

I get the output correctly as

输出是00001111

The output is 00001111

推荐答案

您字面上有一些错误。有警告编译,所建议的,将帮助您注意需要修复的东西。处理最显著第一(编译器的无法找到 g_fopen ),例如:

You literally have a number of errors. Compiling with warnings, as suggested, will help direct your attention to what needs to be fixed. Addressing the most significant first (the compiler's inability to find g_fopen) e.g.:

debug.c:12:2: warning: implicit declaration of function ‘g_fopen’ [-Wimplicit-function-declaration]
  FILE *file = g_fopen("Multicore","w");
  ^
debug.c:12:15: warning: initialization makes pointer from integer without a cast [enabled by default]
  FILE *file = g_fopen("Multicore","w");
           ^

它告诉你你是缺少包括(S)文件。快速检查会告诉你,包括:`

Which tells you you are missing an include(s) file. A quick check would tell you to include: `

#include <glib-object.h>
#include <glib/gstdio.h>

固定包括后,你会发现许多额外的警告地址:

After fixing the includes, you would find a number of additional warnings to address:

debug.c: In function ‘main’:
debug.c:13:20: warning: initialization makes pointer from integer without a cast [enabled by default]
gchar *contents = 00001111;
                    ^
debug.c:16:2: warning: passing argument 2 of ‘g_file_set_contents’ from incompatible pointer type [enabled by default]
g_file_set_contents ("Multicore", &contents, 8, &err);
^
In file included from /usr/include/glib-2.0/glib.h:50:0,
                from debug.c:1:
/usr/include/glib-2.0/glib/gfileutils.h:91:10: note: expected ‘const gchar *’ but argument is of type ‘gchar **’
gboolean g_file_set_contents (const gchar *filename,
        ^
debug.c:28:3: warning: passing argument 1 of ‘g_ascii_digit_value’ makes integer from pointer without a cast [enabled by default]
int p = g_ascii_digit_value(contents);
^
In file included from /usr/include/glib-2.0/glib.h:81:0,
                from debug.c:1:
/usr/include/glib-2.0/glib/gstrfuncs.h:96:23: note: expected ‘gchar’ but argument is of type ‘gchar *’
gint                  g_ascii_digit_value  (gchar    c) G_GNUC_CONST;
                    ^
/usr/include/glib-2.0/glib/gstrfuncs.h:67:19: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
((g_ascii_table[(guchar) (c)] & G_ASCII_DIGIT) != 0)
                ^
debug.c:29:7: note: in expansion of macro ‘g_ascii_isdigit’
if (g_ascii_isdigit (contents))
    ^
debug.c:30:5: warning: return makes integer from pointer without a cast [enabled by default]
    return contents - '0';
    ^
debug.c:32:3: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘gchar *’ [-Wformat=]
g_printf(" The output is %c \n", contents);
^

寻址每个反过来让你到你的程序,只有预警的未使用的变量的,不会影响其运行编译点:

Addressing each in turn gets you to the point that your program compiles with only warning for unused variables which will not effect its operation:

#include <glib.h>
#include <glib-object.h>
#include <glib/gstdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char **argv)
{

    FILE *file = g_fopen ("Multicore", "w");
    gchar *contents = "00001111";
    gchar **contents1 = NULL;
    GError *err = NULL;
    g_file_set_contents ("Multicore", contents, 8, &err);
    g_assert ((contents == NULL && err != NULL)
            || (contents != NULL && err == NULL));
    if (err != NULL) {
        g_assert (contents == NULL);
        fprintf (stderr, "Unable to read file: %s\n", err->message);
        g_error_free (err);
    } else {
        g_assert (contents != NULL);
    }
    int p = g_ascii_digit_value (*contents);
    if (g_ascii_isdigit (*contents))
        return *contents - '0';
    return -1;
    g_printf (" The output is %c \n", *contents);
    return 0;
}

使用

$ ./bin/debug

输出

$ cat Multicore
00001111

根据需要,无需额外的或奇怪的字符。

As desired with no additional or strange characters.

全部编译字符串瓦特/警告启用

这是我使用(openSUSE的13.1)笔记本电脑的完整的编译字符串,利用 pkg配置以确保必要的包括/ lib目录的路径,以及在库本身。所使用的编译字符串是:

The full compile string on the laptop I'm using (openSuSE 13.1), makes use of pkg-config to secure the necessary include/lib paths as well as the libraries themselves. The compile string used was:

gcc -Wall -Wextra -Ofast -o bin/debug debug.c \
`pkg-config --cflags --libs gtk+-2.0`

如果你没有 pkgconfig ,全面编译字符串扩展(使用行延续插入可读性)将是:

If you do not have pkgconfig, the full compile string expanded (with line continuations inserted for readability) would be:

gcc -Wall -Wextra -Ofast -o bin/debug debug.c -pthread -I/usr/include/gtk-2.0 \
-I/usr/lib64/gtk-2.0/include -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 \
-I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/libdrm \
-I/usr/include/libpng16 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng16 \
-I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1.0 \
-I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/freetype2 \
-lgtk-x11-2.0 -lgdk-x11-2.0 -lpangocairo-1.0 -latk-1.0 -lcairo -lgdk_pixbuf-2.0 \
-lgio-2.0 -lpangoft2-1.0 -lpango-1.0 -lgobject-2.0 -lglib-2.0 -lfontconfig \
-lfreetype

这篇关于使用巧舌如簧写在文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆