1

I want to dynamicly construct a matrix in C from a text document using a function. I ran into problems when making the matrix using calloc and probably when giving the values to the matrice elements, and I couldn't find anything. I can handle a vector.

Code here:

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

void beolvas_ellista(int *, int *, int *, int *, int ***, char *);

int main()
{
    int ir, suly, csom, el, i, **ellista;

    //The following commented code works
    /*FILE * f;
    f=fopen("be.txt","r");

    fscanf(f,"%d",&ir);
    fscanf(f,"%d",&suly);
    fscanf(f,"%d",&csom);
    fscanf(f,"%d",&el);

    ellista=(int **)calloc(2,sizeof(int *));
    for(i=0;i<el;++i)
    {
        ellista[i]=(int *)calloc(el,sizeof(int));
    }
    i=0;
    while(!feof(f))
    {
        fscanf(f,"%d",&ellista[0][i]);
        fscanf(f,"%d",&ellista[1][i]);
        ++i;
    }

    for(i=0;i<el;++i)
        printf("%d %d\n",ellista[0][i],ellista[1][i]);

    fclose(f);*/

    beolvas_ellista(&ir, &suly, &csom, &el, &ellista, "be.txt");

    for(i=0;i<el;++i)
        printf("%d %d\n",ellista[0][i],ellista[1][i]);

    return 0;
}

void beolvas_ellista(int *ir, int *suly, int *csom, int *el, int ***ellista, char *allomany)
{
    int i;
    FILE * f;
    f=fopen(allomany,"r");

    fscanf(f,"%d",ir);
    fscanf(f,"%d",suly);
    fscanf(f,"%d",csom);
    fscanf(f,"%d",el);

    *ellista=(int **)calloc(2,sizeof(int *));
    for(i=0;i<*el;++i)
    {
        *ellista[i]=(int *)calloc(*el,sizeof(int));
    }

    i=0;
    while(!feof(f))
    {
        fscanf(f,"%d",ellista[0][i]);
        fscanf(f,"%d",ellista[1][i]);
        ++i;
    }

    fclose(f);
}

Here is the text file:

be.txt

0 0
7 8
1 2
1 3
2 3
3 4
4 5
4 6
5 7
6 7

Also here is the code that I used to gather information:

void beolvas(int*pn, int**pa, char*allomany)
{
int i;FILE*f;
f=fopen(allomany,"r");
fscanf(f,"%d",pn);
*pa=(int*)malloc((*pn)*sizeof(int));
for(i=0; i<*pn; i++)
fscanf(f,"%d",(*pa)+i);
fclose(f);
}
main()
{
int n, *a;
beolvas(&n, &a, "be.txt");
...
}
4

2 回答 2

0

您的输入文件不符合代码。您在输入文件中说 *el = 8。在您的代码中,您将此作为行数,但实际上在您的第一个 calloc 调用中只将 2 行用于矩阵...

PS试试这个:

编辑:我的错误分析。您的实际问题是优先问题。您需要以正确的方式取消引用您的*并设置括号,如下所示:

void beolvas_ellista(int *ir, int *suly, int *csom, int *el, int ***ellista, char *allomany)
{
    int i;
    FILE * f;
    f=fopen(allomany,"r");

    fscanf(f,"%d",ir);
    fscanf(f,"%d",suly);
    fscanf(f,"%d",csom);
    fscanf(f,"%d",el);


    *ellista=(int **)calloc(2,sizeof(int *));
    for(i=0;i<*el;++i)
    {
        (*ellista)[i]=(int *)calloc(*el,sizeof(int));

    }



    i=0;

    while(!feof(f))
    {
        fscanf(f,"%d",&((*ellista)[0][i]));
        fscanf(f,"%d",&((*ellista)[1][i]));
        ++i;
    }

    fclose(f);
}
于 2013-10-28T18:49:42.163 回答
0

以下项目符号列出了您的函数中错误的项目。

  • 您错误地用作循环feof()的中断条件。有关更多信息,请参阅此问题while

  • 您忽略 和 的返回结果fscanf(),因此无法保证所有参数解析成功与否。请参阅 的文档fscanf()

  • 您的代码不适合文件内容的模型。根据您的代码,该文件应分别设置irsulycsomel0078然后,您为恰好两个指向 int 的指针分配空间,将结果保存在 中ellista,然后继续索引*ellista最多el项目,这显然不是 2. 完成后您是否需要 2xN 矩阵或 Nx2 矩阵既不清楚也不明显,并且所编写的代码也没有正确。

  • 风格,但很有帮助:您应该在函数成功时设置输出参数,而不是在初始入口或解析时。例如:您ellista的按地址参数应该设置为最后一个操作,而不是第一个,基于函数的成功。声明一个本地int** local;临时变量,运行你的算法来填充它,并在成功后设置输出参数。

综上所述,我认为您想要一个 2xN 矩阵,如果是这样,下面的代码将执行此操作。请注意,这不会检查malloc 和 calloc 调用的结果,我将其留给您。此函数将0在成功时返回零 ( ),在失败时返回非零:

int beolvas_ellista(int *ir, int *suly, int *csom, int *el, int ***ellista, const char *allomany)
{
    FILE * f = fopen(allomany,"r");
    int ** local =  NULL;
    int i, res = -1;

    if (f == NULL)
        return res;

    if (fscanf(f,"%d",ir) == 1 &&
        fscanf(f, "%d",suly) == 1 &&
        fscanf(f,"%d",csom) == 1 &&
        fscanf(f,"%d",el) == 1)
    {
        // allocate two pointers, then in those two pointers, allocate
        //  space for *el integers.
        local = malloc(2 * sizeof(*local));
        local[0] = calloc(*el, sizeof(*(local[0])));
        local[1] = calloc(*el, sizeof(*(local[0])));

        for (i=0; i<*el; ++i)
        {
            if (fscanf(f, "%d", local[0]+i) != 1 ||
                fscanf(f, "%d", local[1]+i) != 1)
                break;
        }

        // only if i == *el did we finish the above
        if (i == *el)
        {
            *ellista = local;
            res = 0;
        }
        else
        {   // failed to read file content. free up memory
            // and return error state.
            free(local[0]);
            free(local[1]);
            free(local);
        }
    }
    fclose(f);
    return res;
}
于 2013-10-28T19:48:31.860 回答