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

时间:2014-07-23 16:14:21

标签: c file fopen fclose

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

发布我的"测试"代码:

#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;
}

现在这是存储其他纯文本文件路径的主文件: 的 lol.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

我在&#34; engine_start&#34;的第一次迭代之后得到了SIGSEGV,可能是由于FILE *解析器(但我可能完全错了,我现在还不知道)。

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

1 个答案:

答案 0 :(得分:1)

进行以下更改并尝试 -

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;
}