分割错误(核心转储,同时尝试打开多个文件以从中读取文件

问题描述 投票:0回答:2

我有三个文件(使用python .tofile函数编写),其中包含1024个512数组(双值)。我写了这个C程序来打开文件并将值加载到内存数组中,以便我可以按索引对其进行访问。如果我只尝试打开一个文件,而又不尝试读取另一个文件,则该文件可以正常运行并为我提供所需的值。例如,此代码似乎很吸引人

#include <stdio.h>
#include <stdlib.h>
#define xres 1024
#define yres 512
#define xgrid_spacing 0.025                                   //grid spacing along x
#define ygrid_spacing 0.025                                   //grid spacing along y
#define zgrid_spacing 1.0                                     //grid spacing along z

#define xbeg -12.8                                            //X coordinate limits
#define xend 12.8
#define ybeg -6.4                                             //Y coordinate limits
#define yend 6.4
#define zbeg 0.0                                              //Z coordinate limits
#define zend 1.0
void main(){

    const char file0[] = "bx1_1024X512.bin";
    const char file1[] = "bx2_1024X512.bin";
    const char file2[] = "v_crs_b_1024X512.bin";
    FILE *inp0;
    FILE *inp1;
    FILE *inp2;
double xx, yy, zz, xgrid_len, ygrid_len, zgrid_len;
    float xfloat, yfloat, zfloat;
    int xindex, yindex, zindex;

    //index counters
    int iind, jind, ind, jnd;
    //open the file

    double bx_elem, by_elem, bz_elem, v_crs_b_elem;
    inp0 = fopen(file0,"rb");
    inp1 = fopen(file1,"rb");
    inp2 = fopen(file2,"rb");
    //array to store the values
    double bxarray[xres][yres];
    double byarray[xres][yres];
    double minusv_crs_b_array[xres][yres];
    //iterate through the elements and save them to a memory array in C 

    for (iind=0;iind<xres;iind++)
    {
        for (jind=0;jind<yres;jind++)
        {
            fread(&bx_elem, sizeof(double), 1, inp0);
            bxarray[iind][jind]= bx_elem;

        }

    }

    //print the value to check
    printf("%lf\n", bxarray[0][0]);
    //close the file
    fclose(inp0);

}

但是一旦我尝试读取两个或更多个文件,如下所示

#include <stdio.h>
#include <stdlib.h>
#define xres 1024
#define yres 512
#define xgrid_spacing 0.025                                   //grid spacing along x
#define ygrid_spacing 0.025                                   //grid spacing along y
#define zgrid_spacing 1.0                                     //grid spacing along z

#define xbeg -12.8                                            //X coordinate limits
#define xend 12.8
#define ybeg -6.4                                             //Y coordinate limits
#define yend 6.4
#define zbeg 0.0                                              //Z coordinate limits
#define zend 1.0
void main(){

    const char file0[] = "bx1_1024X512.bin";
    const char file1[] = "bx2_1024X512.bin";
    const char file2[] = "v_crs_b_1024X512.bin";
    FILE *inp0;
    FILE *inp1;
    FILE *inp2;
double xx, yy, zz, xgrid_len, ygrid_len, zgrid_len;
    float xfloat, yfloat, zfloat;
    int xindex, yindex, zindex;

    //index counters
    int iind, jind, ind, jnd;
    //open the file

    double bx_elem, by_elem, bz_elem, v_crs_b_elem;
    inp0 = fopen(file0,"rb");
    inp1 = fopen(file1,"rb");
    inp2 = fopen(file2,"rb");
    //array to store the values
    double bxarray[xres][yres];
    double byarray[xres][yres];
    double minusv_crs_b_array[xres][yres];
    //iterate through the elements and save them to a memory array in C 

    for (iind=0;iind<xres;iind++)
    {
        for (jind=0;jind<yres;jind++)
        {
            fread(&bx_elem, sizeof(double), 1, inp0);
            bxarray[iind][jind]= bx_elem;
            fread(&by_elem, sizeof(double), 1, inp1);
            byarray[iind][jind]= by_elem;

        }

    }

    //print the value to check
    printf("%lf\n", bxarray[10][30]);
    //close the file
    fclose(inp0);
    fclose(inp1);

}

我收到分段错误(核心转储)错误。为了澄清起见:该文件与代码

位于同一目录中
c file-io segmentation-fault dynamic-arrays
2个回答
0
投票

结果证明,堆栈大小是问题,为了安全起见,将ulimit -s 102400(100MB)设置为问题,解决了该问题!

© www.soinside.com 2019 - 2024. All rights reserved.