簡體   English   中英

使用glib寫入文件的數據

[英]Data written in file using glib

我有一個代碼,使用g_file_set_contents在文件中寫入許多長度為<length>的字符。 當我打開文件時,我看到一些看起來像ASCII的奇怪字符,例如@&@@。 我假設數據可能是以ASCII格式寫入的,是從二進制轉換的,所以我使用了一個將ASCII轉換為二進制的函數。 執行后我仍然沒有得到任何解決。
這是代碼

#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;
}

我正確地獲得了輸出

輸出為00001111

您實際上有許多錯誤。 如建議的那樣,使用警告進行編譯將有助於您將注意力集中在需要解決的問題上。 首先解決最重要的問題(編譯器無法找到g_fopen ),例如:

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");
           ^

這表明您缺少一個包含文件。 快速檢查會告訴您包括:

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

修復了包含內容后,您會發現一些其他警告,以解決:

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);
^

依次解決每個問題,您將到達程序編譯的地步,僅警告未使用的變量 ,這不會影響其操作:

#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

根據需要,沒有其他或奇怪的字符。

啟用警告的完整編譯字符串

我正在使用的筆記本電腦(openSuSE 13.1)上的完整編譯字符串利用pkg-config來保護必需的include / lib路徑以及庫本身。 使用的編譯字符串為:

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

如果沒有pkgconfig ,則擴展的完整編譯字符串(為了便於閱讀,插入了行繼續符)將是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM