C 使用glib写入文件的数据
我有一个代码,它使用g_file_set_内容在一个文件中写入许多长度为C 使用glib写入文件的数据,c,glib,C,Glib,我有一个代码,它使用g_file_set_内容在一个文件中写入许多长度为的字符。当我打开文件时,我看到一些奇怪的字符,看起来像是ASCII格式的@&@。我假设数据可能是以ASCII格式编写的,从二进制转换而来,所以我使用了一个函数将ASCII转换为二进制。执行后我仍然没有得到任何决议。 这是密码 #include <glib.h> #include <stdio.h> #include <stdlib.h> #include <string.h>
的字符。当我打开文件时,我看到一些奇怪的字符,看起来像是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;
}
#包括
#包括
#包括
#包括
int main(int argc,字符**argv)
{
FILE*FILE=g_fopen(“多核”、“w”);
gchar*内容=00001111;
gchar**contents1=NULL;
GError*err=NULL;
g_文件_集合_内容(“多核”、&contents、8、&err);
g_assert((contents==NULL&&err!=NULL)| |(contents!=NULL&&err==NULL));
if(err!=NULL)
{
g_断言(contents==NULL);
fprintf(stderr,“无法读取文件:%s\n”,错误->消息);
g_无错误(err);
}
其他的
{
g_断言(内容!=NULL);
}
INTP=g_ascii_数字_值(内容);
if(g_ascii_isdigit(目录))
返回内容-“0”;
返回-1;
g_printf(“输出为%c\n”,内容);
返回0;
}
我得到了正确的输出
输出为00001111您实际上有许多错误。按照建议,使用警告进行编译将有助于将您的注意力引导到需要修复的问题上。解决最重要的第一个问题(编译器无法找到
g_fopen
),例如:
这表明您缺少一个包含文件。快速检查会告诉您包括:`
#include <glib-object.h>
#include <glib/gstdio.h>
依次寻址每个变量会使您的程序编译时只对未使用的变量发出警告,这不会影响其操作:
#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;
}
输出
$ 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
你确实有很多错误。按照建议,使用警告进行编译将有助于将您的注意力引导到需要修复的问题上。解决最重要的第一个问题(编译器无法找到
g_fopen
),例如:
这表明您缺少一个包含文件。快速检查会告诉您包括:`
#include <glib-object.h>
#include <glib/gstdio.h>
依次寻址每个变量会使您的程序编译时只对未使用的变量发出警告,这不会影响其操作:
#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;
}
输出
$ 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
您可能希望使用
-Wall-Werror
进行编译,并在生成代码时修复警告,我得到大约12条警告,这些警告看起来非常严重,足以产生意外结果。我使用此命令。gccpkg-config--cflags glib-2.0
new_compile.cpkg-config--libs glib-2.0
我应该在命令末尾加上-Wall和-Werror吗?显然,我遇到了12个13个之前没有出现的错误。glibc
通常指GNULibcglib
是GTK的一个软件包(无需GUI即可使用),您可能希望使用-Wall-Werror
进行编译,并在构建代码时修复警告,我收到大约12条警告,这些警告看起来严重到足以产生意外结果。我使用此命令。gccpkg-config--cflags glib-2.0
new_compile.cpkg-config--libs glib-2.0
我应该在命令末尾加上-Wall和-Werror吗?显然,我遇到了12个13个之前没有出现的错误。glibc
通常指GNULibcglib
是GTK的一个软件包(无需GUI即可使用)您好,David——我知道您通常做得很好。OP问[我]关于墙的位置。我本来打算做一个类似的“全方位服务”清理并发布,但现在,没有必要了。我会添加一个关于如何创建合理的Makefile的附录。也许,你可以把这个添加到你的帖子中,因为我不值得单独回答这个问题。好吧,我将发布编译字符串。我使用了一个不同的系统,通过构建脚本在每个目录上动态创建一个makefile,本地bldflags
包含必要的include和lib路径,因此我没有完整的makefile:)
我做了一些非常类似的事情[事实上,完全相似——用perl编写的构建系统与scons
]有相似之处。如果我遇到你,我可能会称你为“朋友”:-)。我只说Makefiles。我已经建立了一个rules.mk,但它仍然需要一点perl基础设施,这就是我没有发布它的原因。它不完整,令人尴尬,但我把makefile的一个副本放在了。我只是在当前目录中放了一个bldflags
文件,其中包含需要的其他标志/libs项目。对于我的gtk2目录,我在bldflags
文件中有以-pthread…
开头的所有内容。请给我您的任何建议:)这需要一点时间,但我会看看。我已经构建了一段时间的perl脚本基础结构(例如250000行)。构建部分是10000行,但核心库是30000行。它只需一个命令即可安装,但…不太可移植。但是,我发布了构建脚本的核心定义文件。这些文件是.ini st