[英]Access Violation error when using fscanf to read values from .txt file into a dynamically allocated array in C?
我正在尝试编写一个 function,它将 N 个主体的 position 个向量(pos_x、pos_y、pos_z)存储到一个动态数组中。 但是,使用 fscanf 将值读入数组时会出现访问冲突错误。 我不确定这是因为 memory 分配不正确(它应该是正确的,因为它是从另一个函数中获取的),或者值读取不正确。 initial_positions.txt 文件中的 position 向量采用以下格式(不带标题):
pos_x, pos_y, pos_z
1 2 3
4 5 6
7 8 9
1 2 3
4 5 6
我的function:
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;
}
非常感谢任何帮助:)
您在第一个循环中分配并显示未初始化的数据,但在尝试将数据读入数组之前不要重置row
和column
,以免写出界。
您的列边界不正确(应该是3
而不是numberOfBodies
)。
读取数据时,您同时使用列和手动索引。 由于您没有遍历列,因此只需对列索引进行硬编码。
检查fscanf()
是否实际读取了您预期的 3 个字段,否则这些变量未初始化。
在返回调用方时不要释放刚刚读取的数据。
使用常量而不是硬编码魔法值 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);
}
output 是:
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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.