c - 使用 C 中的结构将数据存储到动态数组中

标签 c arrays file struct

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

我想将这两个整数存储到类型为 int 的变量中然后将它们存储到 point 中在我的 list 中的数组里面结构。 我对拥有两个结构感到非常困惑,并且不确定这是否是正确的方法。欢迎任何反馈。

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

最佳答案

首先你的代码不能编译因为这个

p->A[n] = P.x;
p->A[n] = P.y;

错了,应该是

p->A[n].x = P.x;
p->A[n].y = P.y;

因为 A 的类型是 point 并且您应该访问该结构的成员以便为它们赋值。

但这只是问题的开始,您没有为 A 指针分配空间,所以这不会起作用。

  1. 需要为list类型的实例分配空间,就是这样完成的

    p = malloc(sizeof(*p));
    
  2. 然后你需要初始化p的成员,为此

    p->values   = malloc(N * sizeof(point));
    p->capacity = N;
    p->size     = 0;
    

    如您所见,空间已分配给 values 成员。

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

  4. 在两个int变量中捕获从文件中扫描的值,只有在成功读取的地方才将它们复制到数组中

    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;
    }
    
  5. 检查文件是否打开。

我建议使用以下代码

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

如您所见,您可以对代码进行另一项改进,这不是很重要,但可以避免使用不必要的 Nn 变量.

注意:在使用函数之前,请尝试通读它的文档,这将防止出现各种意外结果,例如 fscanf() ,将帮助您更多地了解我的修复。

关于c - 使用 C 中的结构将数据存储到动态数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28706997/

相关文章:

c - 非常简单的地穴程序错误

C 中冲突的类型和变量命名

Java 迭代固定长度的数组并使用 Scanner 类获取值

c# - 使用数组在 C# 中制作地址簿

java - 如何格式化和比较来自 bash/vm 的输入?

file - 响应传输文件

c - 使用滚动条调整窗口大小时出现奇怪的效果

使用数组堆栈将后缀转换为中缀表示法

javascript - 如何迭代数组中的数组

java - 使用 JAVA 控制台应用程序从桌面访问文本文件