Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C语言中的结构将数据存储到动态数组中_C_Arrays_File_Struct - Fatal编程技术网

用C语言中的结构将数据存储到动态数组中

用C语言中的结构将数据存储到动态数组中,c,arrays,file,struct,C,Arrays,File,Struct,我在将文件中的数据存储到动态数组时遇到问题。我知道我现在所拥有的是不正确的,但它只是暂时存在的。我有一个文件,第一行包含大量的数据行。以下几行有两个并排的整数来表示有序对。我想将这两个整数存储到一个结构中,point,它表示有序对。此外,还有一个数组具有这样一个结构,它位于另一个结构的内部,list,该结构包含数组的大小或当前存储在数组中的数据量,以及一个容量,即数组中的总空间量 我想将这两个整数存储到int类型的变量中,然后将它们存储到列表结构中数组内部的点。 我对有两个结构感到非常困惑,不确

我在将文件中的数据存储到动态数组时遇到问题。我知道我现在所拥有的是不正确的,但它只是暂时存在的。我有一个文件,第一行包含大量的数据行。以下几行有两个并排的整数来表示有序对。我想将这两个整数存储到一个结构中,
point
,它表示有序对。此外,还有一个数组具有这样一个结构,它位于另一个结构的内部,
list
,该结构包含数组的大小或当前存储在数组中的数据量,以及一个容量,即数组中的总空间量

我想将这两个整数存储到
int
类型的变量中,然后将它们存储到
列表
结构中数组内部的
。 我对有两个结构感到非常困惑,不确定这是否是正确的方法。欢迎任何反馈

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

typedef struct
{
    int x;
    int y;
} point;

typedef struct
{
    int size;
    int capacity;
    point *A;
} list;

// Compute the polar angle in radians formed
// by the line segment that runs from p0 to p
double polarAngle(point p, point p0)
{
    return atan2(p.y - p0.y, p.x - p0.x);
}

// Determine the turn direction around the corner
// formed by the points a, b, and c. Return a
// positive number for a left turn and negative
// for a right turn.
double direction(point a, point b, point c)
{
    return (b.x - a.x)*(c.y - a.y) - (c.x - a.x)*(b.y - a.y);
}

int whereSmallest(point A[], int begin, int end, point p0)
{
    point min = A[begin];
    int where = begin;
    int n;
    for (n = begin + 1; n < end; n++)
        if (polarAngle(A[n], p0) < polarAngle(min, p0))
        {
            min = A[n];
            where = n;
        }
    return where;
}
void selectionSort(point A[], int N, point p0)
{
    int n, s;
    point temp;
    for (n = 0; n < N; n++)
    {
        s = whereSmallest(A, n, N, p0);
        temp = A[n];
        A[n] = A[s];
        A[s] = temp;
    }
}

// Remove the last item from the list
void popBack(list *p)
{
    int x;
    x = p->size - 1;
    p->A[x] = p->A[x + 1];
}

// Return the last item from the list
point getLast(list *p)
{
    point value;
    value = p->A[p->size];
    return value;
}

// Return the next to the last item
point getNextToLast(list *p)
{
    point value;
    value = p->A[p->size - 1];
    return value;
}

int main(int argc, const char *argv[])
{
    point p0, P;
    FILE *input;
    list *p;
    int N, n, x, y;

    /*Assuming that the first piece of data in the array indicates the amount of numbers in the array then we record this number as a reference.*/
    N = 0;
    input = fopen("points.txt", "r");
    fscanf(input, "%d", &N);

    /*Now that we have an exact size requirement for our array we can use that information to create a dynamic array.*/
    p = (point*)malloc(N*sizeof(point));
    if (p == NULL)//As a safety precaution we want to terminate the program in case the dynamic array could not be successfully created.
        return -1;

    /*Now we want to collect all of the data from our file and store it in our array.*/
    for (n = 0; n < N; n++)
    {
        fscanf(input, "%d %d", &P.x, &P.y);
        p->A[n] = P.x;
        p->A[n] = P.y;
    }
    fclose(input);

    free(p);
    return 0;
}
#包括
#包括
#包括
类型定义结构
{
int x;
int-y;
}点;
类型定义结构
{
整数大小;
国际能力;
第*A点;
}名单;
//以形成的弧度计算极角
//通过从p0到p的线段
双极化(p点,p0点)
{
返回atan2(p.y-p0.y,p.x-p0.x);
}
//确定拐角处的转弯方向
//由点a、b和c构成。归还
//左转为正数,左转为负数
//右转。
双向(点a、点b、点c)
{
报税表(b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
最小点(点A[],点开始,点结束,点p0)
{
最小点=A[开始];
int其中=开始;
int n;
对于(n=begin+1;nsize-1;
p->A[x]=p->A[x+1];
}
//返回列表中的最后一项
点getLast(列表*p)
{
点值;
值=p->A[p->size];
返回值;
}
//将下一项返回到最后一项
点getNextToLast(列表*p)
{
点值;
值=p->A[p->size-1];
返回值;
}
int main(int argc,const char*argv[]
{
p0点,P;
文件*输入;
列表*p;
int N,N,x,y;
/*假设数组中的第一条数据指示数组中的数字量,那么我们将该数字记录为引用*/
N=0;
输入=fopen(“points.txt”、“r”);
fscanf(输入,“%d”&N);
/*现在我们对数组有了精确的大小要求,我们可以使用这些信息创建动态数组*/
p=(点*)malloc(N*sizeof(点));
if(p==NULL)//作为安全预防措施,我们希望在无法成功创建动态数组的情况下终止程序。
返回-1;
/*现在,我们希望从文件中收集所有数据,并将其存储在数组中*/
对于(n=0;nA[n]=p.x;
p->A[n]=p.y;
}
fclose(输入);
自由基(p);
返回0;
}

首先,您的代码无法编译,因为

p->A[n] = P.x;
p->A[n] = P.y;
这是错误的,应该是错误的

p->A[n].x = P.x;
p->A[n].y = P.y;
因为
A
具有类型
,您应该访问结构的成员以便为其赋值

但这只是问题的开始,您没有为
A
指针分配空间,因此这将不起作用

  • 您需要为类型为
    list
    的实例分配空间,这是通过这种方式完成的

    p = malloc(sizeof(*p));
    
  • 然后您需要初始化
    p
    的成员,对于

    p->values   = malloc(N * sizeof(point));
    p->capacity = N;
    p->size     = 0;
    
    如您所见,为
    成员分配了空间

  • 检查
    fscanf()
    以确保数据完整性并避免未定义的行为,如果
    fscanf()
    失败,您将永远无法知道您的代码,并且您可能访问未初始化的变量,从而导致未定义的行为

  • 在两个
    int
    变量中捕获从文件扫描的值,并仅在where成功读取时将其复制到数组中

    for (n = 0 ; ((n < N) && (fscanf(input, "%d%d", &x, &y) == 2)) ; n++)
    /* check that the values were read from the file _______^ */
    {
        /* store them in the array */
        p->values[n].x = x;
        p->values[n].y = y;
        p->size       += 1;
    }
    
    for(n=0;((n值[n].x=x;
    p->值[n].y=y;
    p->size+=1;
    }
    
  • 检查文件是否已打开

  • 我建议使用以下代码

    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    typedef struct
    {
        int x;
        int y;
    } point;
    
    typedef struct
    {
        int size;
        int capacity;
        point *values;
    } list;
    
    // Compute the polar angle in radians formed
    // by the line segment that runs from p0 to p
    double polarAngle(point p, point p0)
    {
        return atan2(p.y - p0.y, p.x - p0.x);
    }
    
    // Determine the turn direction around the corner
    // formed by the points a, b, and c. Return a
    // positive number for a left turn and negative
    // for a right turn.
    double direction(point a, point b, point c)
    {
        return (b.x - a.x)*(c.y - a.y) - (c.x - a.x)*(b.y - a.y);
    }
    
    int whereSmallest(point values[], int begin, int end, point p0)
    {
        point min = values[begin];
        int where = begin;
        int n;
        for (n = begin + 1; n < end; n++)
            if (polarAngle(values[n], p0) < polarAngle(min, p0))
            {
                min = values[n];
                where = n;
            }
        return where;
    }
    void selectionSort(point values[], int N, point p0)
    {
        int n, s;
        point temp;
        for (n = 0; n < N; n++)
        {
            s         = whereSmallest(values, n, N, p0);
            temp      = values[n];
            values[n] = values[s];
            values[s] = temp;
        }
    }
    
    // Remove the last item from the list
    void popBack(list *p)
    {
        int x;
        x = p->size - 1;
        p->values[x] = p->values[x + 1];
    }
    
    // Return the last item from the list
    point getLast(list *p)
    {
        point value;
        value = p->values[p->size];
        return value;
    }
    
    // Return the next to the last item
    point getNextToLast(list *p)
    {
        point value;
        value = p->values[p->size - 1];
        return value;
    }
    
    int main(int argc, const char *argv[])
    {
        FILE *input;
        list *p;
        int   N, n, x, y;
    
        /*Assuming that the first piece of data in the array indicates the amount of numbers in the array then we record this number as a reference.*/
        N     = 0;
        input = fopen("points.txt", "r");
        if (input == NULL)
            return -1;
        if (fscanf(input, "%d", &N) != 1)
        {
            fclose(input);
            return -1;
        }
    
        p = malloc(sizeof(*p));
        if (p == NULL)
            return -1;
    
        /*Now that we have an exact size requirement for our array we can use that information to create a dynamic array.*/
        p->values   = malloc(N * sizeof(point));
        p->capacity = N;
        p->size     = 0;
        if (p->values == NULL)//As a safety precaution we want to terminate the program in case the dynamic array could not be successfully created.
        {
            free(p);
            fclose(input);
    
            return -1;
        }
    
        /*Now we want to collect all of the data from our file and store it in our array.*/
        for (n = 0 ; ((n < N) && (fscanf(input, "%d%d", &x, &y) == 2)) ; n++)
        {
            p->values[n].x = x;
            p->values[n].y = y;
            p->size       += 1;
        }
        fclose(input);
    
        free(p->values);
        free(p);
        return 0;
    }
    
    #包括
    #包括
    #包括
    类型定义结构
    {
    int x;
    int-y;
    }点;
    类型定义结构
    {
    整数大小;
    国际能力;
    点*值;
    }名单;
    //以形成的弧度计算极角
    //通过从p0到p的线段
    双极化(p点,p0点)
    {
    返回atan2(p.y-p0.y,p.x-p0.x);
    }
    //确定拐角处的转弯方向
    //由点a、b和c构成。归还
    //左转为正数,左转为负数
    //右转。
    双向(点a、点b、点c)
    {
    报税表(b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
    }
    int其中最小(点值[],int开始,int结束,点p0)
    {
    最小点=值[开始];
    int其中=开始;
    int n;
    对于(n=begin+1;nfor (n = 0; n < N; n++)
    {
        fscanf(input, "%d %d", &(p.A[n].x), &(p.A[N].y));
    }