c - 尝试同时打开多个文件以从中读取时出现段错误(核心转储)

标签 c file-io segmentation-fault dynamic-arrays

我有三个文件(使用 python .tofile 函数编写),其中包含 1024x512 数组和(双)值。我编写了这个 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);

}

我收到段错误(核心转储)错误。 澄清一下:该文件与代码位于同一目录中

最佳答案

事实证明,堆栈大小是问题所在,为了安全起见设置 ulimit -s 102400 (100MB),解决了问题!!

关于c - 尝试同时打开多个文件以从中读取时出现段错误(核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59501112/

相关文章:

c - 将文件读入链表

C 程序 - 在编译器中标记为未声明的结构

c++ - 如何单独处理子ListBox WndProc?

c++ - 从文件中读取任意长度的字节到字符串 C++

c++ - 在 C++ 中读一个词后读一些东西

C DNSlookup抛出段错误

C:为什么我在一种情况下出现段错误,而在另一种情况下却没有?

np.loadtxt 和 iter_loadtxt 中的 Python MemoryError 或 ValueError

c++ - 使用 strcpy 时重复出现段错误

c++ - 为什么这个程序在调用函数时会出现段错误?