C语言 分段错误(核心转储),同时尝试一起打开多个文件以从中读取



我有三个文件(使用 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("%lfn", 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("%lfn", bxarray[10][30]);
//close the file
fclose(inp0);
fclose(inp1);
}

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

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

相关内容

  • 没有找到相关文章

最新更新