使用fscanf将值从.txt文件读取到C?中动态分配的数组时出现访问冲突错误,

l7wslrjt  于 2022-12-17  发布在  其他
关注(0)|答案(1)|浏览(158)

我在写一个函数来存储位置向量(pos_x,pos_y,pos_z)到动态数组中,但是使用fscanf将值读入数组时出现访问冲突错误,不确定是否是内存分配错误(它应该是正确的,因为它是从另一个函数中获取的),或者值被错误地读取。initial_positions.txt文件中的位置矢量是这样的格式(没有头):

pos_x, pos_y, pos_z

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

我的职务:

int main()
{
int numberOfBodies = 5;
ReadInitialPositions(numberOfBodies);
return 0;
}

double **ReadInitialPositions(int numberOfBodies)
{
int row = 0;
int column = 0;
double **initialPositions = malloc(sizeof(double*) * numberOfBodies);

for (row = 0; row < numberOfBodies; row++)
    {
    initialPositions[row] = malloc(sizeof(double) * 3);
    }

for (row = 0; row < numberOfBodies; row++)
    {
    for (column = 0; column < numberOfBodies; column++)
        {
        printf(" %lf", initialPositions[row][column]);
        }
    printf("\n");
    }

FILE *file = fopen("initial_positions.txt", "r");

if (file == NULL)
    {
    printf("\nUnable to access the 'initial_positions.txt' file.\n");
    exit(1);
    }
else
    {
    while (fscanf(file, "%lf %lf %lf", &initialPositions[row][column], &initialPositions[row][column + 1], &initialPositions[row][column + 2]) != EOF)
        {
        printf("%lf", initialPositions[row][column]);
        row++;
        }
    }

fclose(file);
free(initialPositions);
return initialPositions;
}

真的很感激任何帮助:)

6pp0gazn

6pp0gazn1#

1.您在第一个循环中分配并显示未初始化的数据,但在尝试将数据读入数组之前不重置rowcolumn,因此写入超出了界限。
1.列边界不正确(应为3,而不是numberOfBodies)。
1.阅读数据时,同时使用列索引和手动索引。由于不对列进行迭代,因此只需对列索引进行硬编码。
1.检查fscanf()是否确实读取了您期望的3个字段,否则这些变量未初始化。
1.不要释放刚刚读取的数据,因为您正在将其返回给调用方。
1.使用常量而不是硬编码幻值3。

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

#define COLS 3

double **ReadInitialPositions(int numberOfBodies) {
    double **initialPositions = malloc(numberOfBodies * sizeof(double *));
    for (int row = 0; row < numberOfBodies; row++) {
        initialPositions[row] = malloc(COLS * sizeof(double));
    }

    FILE *file = fopen("initial_positions.txt", "r");
    if (!file) {
        printf("\nUnable to access the 'initial_positions.txt' file.\n");
        exit(1);
    }
    for(int row = 0; row < numberOfBodies; row++) {
        int rv = fscanf(file, "%lf %lf %lf", &initialPositions[row][0], &initialPositions[row][1], &initialPositions[row][2]);
        if(rv == EOF) break;
        if(rv != 3) {
            printf("error\n");
            break;
        }
        for (int column = 0; column < COLS; column++) {
            printf(" %lf", initialPositions[row][column]);
        }
        printf("\n");
    }
    fclose(file);
    return initialPositions;
}

int main() {
    double **d = ReadInitialPositions(5);
}

输出为:

1.000000 2.000000 3.000000
 4.000000 5.000000 6.000000
 7.000000 8.000000 9.000000
 1.000000 2.000000 3.000000
 4.000000 5.000000 6.000000

下面是一个更通用的版本,它可以计算出文件中有多少行:

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

#define COLS 3

int ReadInitialPositions(const char *pathname, double (**a)[COLS], size_t *rows) {
    FILE *file = fopen(pathname, "r");
    if (!file) {
        printf("\nUnable to access the '%s' file.\n", pathname);
        return 1;
    }
    *a = NULL;
    for(*rows = 0;; (*rows)++) {
        double x, y, z;
        int rv = fscanf(file, "%lf %lf %lf", &x, &y, &z);
        if(rv == EOF) {
            break;
        }
        if(rv != 3) {
            printf("error\n");
            break;
        }
        double (*tmp)[COLS] = realloc(*a, sizeof(double[(*rows)+1][COLS]));
        if(!*tmp) {
            printf("realloc failed\n");
            fclose(file);
            return 1;
        }
        *a = tmp;
        (*a)[*rows][0] = x;
        (*a)[*rows][1] = y;
        (*a)[*rows][2] = z;
    }
    return 0;
}

int main() {
    double (*a)[COLS];
    size_t rows;
    if(ReadInitialPositions("initial_positions.txt", &a, &rows)) {
        printf("ReadInitialPositions() failed\n");
        return 1;
    }
    for(size_t row = 0; row < rows; row++) {
        for (size_t column = 0; column < 3; column++) {
            printf(" %lf", a[row][column]);
        }
        printf("\n");
    }
}

相关问题