gpt4 book ai didi

C - 使用相同的指针打开不同的文件

转载 作者:行者123 更新时间:2023-11-30 15:31:18 27 4
gpt4 key购买 nike

我正在尝试通过许多纯文本文件检索信息,然后将其存储在正确的结构中。为此,我使用一个函数,该函数接受要填充的结构成员和存储信息的纯文本文件的源。

发布我的“测试”代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct _elem
{
const char *title;
int ok;
int almost;
int nope;
int hits;
float last_rank;
};

typedef struct _elem Chapter;

Chapter *generate_array(const char *source, int *elems);
int engine_start(Chapter *elem, char *source);

int main()
{
const char path_f[100];
int elements = 0;
int i = 0;

Chapter *dict;

printf("Insert the name of the source:\n");
scanf("%s", path_f);

printf("\nGenerating dictionary, please wait...\n");
dict = generate_array(path_f, &elements);

if (dict == NULL)
{
printf("Aborting.\n");
exit(1);
}

while (i < elements)
{
printf("Element %d:\n", (i + 1));
printf("\nTitle: %s\n", dict[i].title);
printf("Ok: %10d\n", dict[i].ok);
printf("Almost: %5d\n", dict[i].almost);
printf("Nope: %8d\n", dict[i].nope);
printf("Hits: %8d\n", dict[i].hits);
printf("Rank: %8.2f\n", dict[i].last_rank);
printf("\n");
i++;
}

return EXIT_SUCCESS;
}

Chapter *generate_array(const char *source, int *elems)
{
FILE *src;

int sources;
int i = 0;

char **srcs;

Chapter *generated;

src = fopen(source, "r");

if (src == NULL)
{
printf("[!!] Error while reading file!\n");
return NULL;
}

fscanf(src, "%d", &sources);

if (sources <= 0)
{
printf("[!!] Wrong number of sources, exiting.\n");
return NULL;
}

srcs = (char **) malloc(sizeof(char *) * sources);

while (i < sources && !feof(src))
{
srcs[i] = (char *) malloc(sizeof(char) * 100);
fscanf(src, "%s", srcs[i++]);
}

fclose(src);

generated = (Chapter *) malloc(sizeof(Chapter) * i);
*elems = i;
i = 0;

while (i < *elems)
{
if(engine_start( &generated[i], srcs[i] )) i++;
else
{
printf("[!!] Error in file %s, aborting.\n", srcs[i]);
return NULL;
}
}

return generated;
}

int engine_start(Chapter *elem, char *source)
{
FILE *parser;
int done = 0;

parser = fopen(source, "r");

if (parser == NULL) printf("[!!] Error while opening %s, aborting.\n", source);
else
{
fgets(elem->title, 100, parser);
fscanf(parser, "%d %d %d %d %f", &(elem->ok), &(elem->almost),
&(elem->nope), &(elem->hits),
&(elem->last_rank) );

fclose(parser);
done = 1;
}

return done;
}

现在这是主文件,其中存储了其他纯文本文件的路径:哈哈.dat

5
lold/lol1.dat
lold/lol2.dat
lold/lol3.dat
lold/lol4.dat
lold/lol5.dat

还有一个 lolX.dat 的示例:


Qual'è la vittoria di cristo?
3 4 5 12 44.9

在“engine_start”第一次迭代后我收到了 SIGSEGV,可能是由于 FILE *parser(但我可能完全错了,我现在不知道)。

有人可以指导我解决这个问题吗?谢谢。

最佳答案

进行以下更改并尝试 -

struct _elem
{
char *title; // allocate the memory for this.
int ok;
int almost;
int nope;
int hits;
float last_rank;
};

在给元素标题分配内容之前,您需要为其分配内存。

int engine_start(Chapter *elem, char *source)
{
FILE *parser;
int done = 0;

parser = fopen(source, "r");

if (parser == NULL) printf("[!!] Error while opening %s, aborting.\n", source);
else
{
elem->title=(char *)malloc(100); // include this line.
fgets(elem->title, 100, parser);
fscanf(parser, "%d %d %d %d %f", &(elem->ok), &(elem->almost),
&(elem->nope), &(elem->hits),
&(elem->last_rank) );

fclose(parser);
done = 1;
}

return done;
}

关于C - 使用相同的指针打开不同的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24915702/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com