【发布时间】:2020-09-03 05:55:34
【问题描述】:
我的目标是将文件写入磁盘
如果我使用 gio 创建文件
GError *error;
char path[strlen(dirpath)];
sprintf(path, "%s", dirpath); // Create path to the file by copying from another variable
zip_file_t *contentfile = zip_fopen_index(book, index, ZIP_RDONLY);
zip_stat_index(book, index, ZIP_CHECKCONS, &fileinfo);
GFile *gfile = g_file_new_for_path(strcat(path, fileinfo.name));
GFileOutputStream *file = g_file_create(gfile, G_FILE_CREATE_NONE, g_cancellable_new(), &error);
if (error)
printf("Error :%i\n", error->code);
else
zip_fread(contentfile, file, fileinfo.size);
g_error_free(error);
我不能在文件中放任何东西。它只是一个用 0 字节创建的文件。我正在使用 loo 提取存档中的所有文件。因此 index 在zip_file_t *contentfile = zip_fopen_index(book, index, ZIP_RDONLY);。我收到一个段错误:GError 设置在先前 GError 或未初始化内存的顶部。
如果我使用文件
char path[strlen(dirpath)];
sprintf(path, "%s", dirpath); // Create path to the file by copying from another variable
zip_file_t *contentfile = zip_fopen_index(book, index, ZIP_RDONLY);
zip_stat_index(book, index, ZIP_CHECKCONS, &fileinfo);
FILE *file = fopen(strcat(path, fileinfo.name), "wb");
zip_fread(contentfile, file, fileinfo.size);
这会导致分段错误:双重释放或损坏(输出)
如何正确地将 zip_read( 内容写入磁盘上的文件?
【问题讨论】:
-
首先,为
path分配足够的内存:将其更改为char path[strlen(dirpath) + strlen(fileinfo.name) + 1];,并在调用zip_stat_index后将其移动。 -
谢谢,我相信这改进了我的代码,谢谢你的提示