C、GLib:有没有一种方法可以获取大小 - 可以应用于文本文件、二进制文件、文件夹和符号链接?
// gcc z.c -o z $(pkg-config --cflags --libs gtk+-2.0)
#include <string.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
struct tst
{
GtkWidget *win, *vb, *ent, *btn, *lbl;
GtkAccelGroup *acc;
GClosure *cls;
};
static void print_val(struct tst *prg)
{
const char *nam = gtk_entry_get_text(GTK_ENTRY(prg->ent));
char *cont;
g_file_get_contents(nam, &cont, NULL, NULL);
int siz = strlen(cont);
g_printf("%d\n", siz);
}
static void window_new()
{
struct tst *prg = g_new0(struct tst, 1);
prg->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
prg->vb = gtk_vbox_new(TRUE, 0);
prg->ent = gtk_entry_new();
prg->btn = gtk_button_new_with_label("Print!");
prg->lbl = gtk_label_new("Enter the string.");
gtk_container_add(GTK_CONTAINER(prg->win), GTK_WIDGET(prg->vb));
gtk_box_pack_start(GTK_BOX(prg->vb), GTK_WIDGET(prg->ent), FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(prg->vb), GTK_WIDGET(prg->btn), FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(prg->vb), GTK_WIDGET(prg->lbl), FALSE, FALSE, 0);
g_signal_connect_swapped(prg->win, "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect_swapped(prg->btn, "clicked", G_CALLBACK(print_val), prg);
gtk_window_set_title(GTK_WINDOW(prg->win), "Enter the string");
gtk_widget_show_all(GTK_WIDGET(prg->win));
}
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
window_new();
gtk_main();
return 0;
}
使用 g_file_get_contents(),至少在这个示例程序中,我可以获取文本文件的大小,但无法获取二进制文件、文件夹和符号链接的大小。
当我尝试用它来获取二进制文件的大小时,它给出了奇怪的值,而不是二进制文件的大小。
当我尝试用它获取文件夹的大小时,它因分段错误而崩溃。
当我尝试获取符号链接的大小时,它给出了链接引用的常规文件的大小,而不是链接本身。
有没有一种方法可以获取大小 - 可以应用于文本文件、二进制文件、文件夹和符号链接?
// gcc z.c -o z $(pkg-config --cflags --libs gtk+-2.0)
#include <string.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
struct tst
{
GtkWidget *win, *vb, *ent, *btn, *lbl;
GtkAccelGroup *acc;
GClosure *cls;
};
static void print_val(struct tst *prg)
{
const char *nam = gtk_entry_get_text(GTK_ENTRY(prg->ent));
char *cont;
g_file_get_contents(nam, &cont, NULL, NULL);
int siz = strlen(cont);
g_printf("%d\n", siz);
}
static void window_new()
{
struct tst *prg = g_new0(struct tst, 1);
prg->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
prg->vb = gtk_vbox_new(TRUE, 0);
prg->ent = gtk_entry_new();
prg->btn = gtk_button_new_with_label("Print!");
prg->lbl = gtk_label_new("Enter the string.");
gtk_container_add(GTK_CONTAINER(prg->win), GTK_WIDGET(prg->vb));
gtk_box_pack_start(GTK_BOX(prg->vb), GTK_WIDGET(prg->ent), FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(prg->vb), GTK_WIDGET(prg->btn), FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(prg->vb), GTK_WIDGET(prg->lbl), FALSE, FALSE, 0);
g_signal_connect_swapped(prg->win, "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect_swapped(prg->btn, "clicked", G_CALLBACK(print_val), prg);
gtk_window_set_title(GTK_WINDOW(prg->win), "Enter the string");
gtk_widget_show_all(GTK_WIDGET(prg->win));
}
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
window_new();
gtk_main();
return 0;
}
With g_file_get_contents(), at least in this example program, I can get the size of text file, but I can't get the size of binary file, folder, and symbolic link.
When I try to get the size of binary file with that, it gives the weird value, not the size of binary file.
When I try to get the size of folder with that, it crashes with segmentation fault.
And when I try to get the size of symbolic link with that, it gives the size of regular file which link refers, not link itself.
Is there a method to get the size - can be applied to text file, binary file, folder, and symbolic link in common?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常的方法是
注意,使用
lstat()
报告的大小将是符号链接本身的大小,而不是它引用的对象的大小;如果您实际上并不想要符号链接自己的大小,请改用 stat() 。The usual way is
Note that with
lstat()
the size reported will be the size of the symlink itself, not the object it references; if you didn't actually want the symlink's own size, usestat()
instead.您可能必须使用 realpath() 来解析符号链接。之后你就可以使用 fstat() 了。
You might have to use realpath() to resolve the symbolic links. After that you could just use fstat().