如何使用glib(c)写入文件?

发布于 2025-02-03 10:29:57 字数 1246 浏览 3 评论 0原文

我正在尝试使用GLIB将数据保存到文件,但不想写入。它仍然可以创建文件。当它创建文件并试图写入该创建的文件指针时,该程序似乎也崩溃了...

相关代码:

#include "save.h"
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>

GFile *save;
GFileIOStream *stream;

void initSave() {
    char *home = "", *fmt = "%s/.ProgSave.%d", *saveStr = "";
    #ifdef _WIN32
        home = getenv("USERPROFILE");
    #else
        home = getenv("HOME");
    #endif
    saveStr = g_strdup_printf(fmt, home, 123);
    save = g_file_new_for_path(saveStr);
    stream = g_file_open_readwrite(save, NULL, NULL);
    if (!stream) {
        stream = g_file_create_readwrite(save, G_FILE_CREATE_NONE, NULL, NULL);
    }
    g_free(saveStr);
}

void closeSave() {
    g_io_stream_close(G_IO_STREAM(stream), NULL, NULL);
    g_object_unref(stream);
    g_object_unref(save);
}

void writeWin() {
    g_output_stream_write(G_OUTPUT_STREAM(stream), "true", 5, NULL, NULL);
    g_output_stream_flush(G_OUTPUT_STREAM(stream), NULL, NULL);
}

bool getWin() {
    char line[5];
    gsize bytesRead;
    g_input_stream_read_all(G_INPUT_STREAM(stream), line, 5, &bytesRead, NULL, NULL);
    if (strcmp(line, "true") == 0) {
        return true;
    } else {
        return false;
    }
}

I'm trying to save data to a file using glib, but it doesn't want to write. It can still create the file. It also seems when it creates the file, and tries to write to that created file pointer, the program just crashes...

The relevant code:

#include "save.h"
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>

GFile *save;
GFileIOStream *stream;

void initSave() {
    char *home = "", *fmt = "%s/.ProgSave.%d", *saveStr = "";
    #ifdef _WIN32
        home = getenv("USERPROFILE");
    #else
        home = getenv("HOME");
    #endif
    saveStr = g_strdup_printf(fmt, home, 123);
    save = g_file_new_for_path(saveStr);
    stream = g_file_open_readwrite(save, NULL, NULL);
    if (!stream) {
        stream = g_file_create_readwrite(save, G_FILE_CREATE_NONE, NULL, NULL);
    }
    g_free(saveStr);
}

void closeSave() {
    g_io_stream_close(G_IO_STREAM(stream), NULL, NULL);
    g_object_unref(stream);
    g_object_unref(save);
}

void writeWin() {
    g_output_stream_write(G_OUTPUT_STREAM(stream), "true", 5, NULL, NULL);
    g_output_stream_flush(G_OUTPUT_STREAM(stream), NULL, NULL);
}

bool getWin() {
    char line[5];
    gsize bytesRead;
    g_input_stream_read_all(G_INPUT_STREAM(stream), line, 5, &bytesRead, NULL, NULL);
    if (strcmp(line, "true") == 0) {
        return true;
    } else {
        return false;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谎言月老 2025-02-10 10:29:58

这是我解决问题的方式!
写入文件并从中读取

    void write_to_file(const char *name)
    {
        // Beg of file
        FILE *file;
        file = fopen(name, "w");
        if (file == NULL)
        {
            printf("error\n");
        }
        // Write to the file here fputc('c', file);
    
        fclose(file);
    }
    
    int read_file(const char *path)
    {
        GFile *file;
    
        file = g_file_new_for_path(path);
        if (file == NULL)
        {
            g_print("file null\n");
            return 1;
        }
    
        GBytes *file_bytes = g_file_load_bytes(file, NULL, NULL, NULL);
        if (file_bytes == NULL)
        {
            g_print("bytes null\n");
            return 1;
        }
        size_t data_size = 0;
        char *pointer = g_bytes_get_data(file_bytes, &data_size);
        if (pointer == NULL)
        {
            g_print("pointer null\n");
            return 1;
        }
    
        for (size_t i = 0; i < data_size; i++)
        {
            //Do the reading directly on the pointer to the bytes of the file
        }
        
        free(pointer);
        g_object_unref(file);
        return 0;
    }

Here is my way of approaching the problem!
To write to a file and to read from it

    void write_to_file(const char *name)
    {
        // Beg of file
        FILE *file;
        file = fopen(name, "w");
        if (file == NULL)
        {
            printf("error\n");
        }
        // Write to the file here fputc('c', file);
    
        fclose(file);
    }
    
    int read_file(const char *path)
    {
        GFile *file;
    
        file = g_file_new_for_path(path);
        if (file == NULL)
        {
            g_print("file null\n");
            return 1;
        }
    
        GBytes *file_bytes = g_file_load_bytes(file, NULL, NULL, NULL);
        if (file_bytes == NULL)
        {
            g_print("bytes null\n");
            return 1;
        }
        size_t data_size = 0;
        char *pointer = g_bytes_get_data(file_bytes, &data_size);
        if (pointer == NULL)
        {
            g_print("pointer null\n");
            return 1;
        }
    
        for (size_t i = 0; i < data_size; i++)
        {
            //Do the reading directly on the pointer to the bytes of the file
        }
        
        free(pointer);
        g_object_unref(file);
        return 0;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文