使用fclose()时出现分段错误
我是C语言的新手,我正在尝试将我的输出写入一个文件。当我试图关闭输出文件时,似乎遇到了分段错误。我按照sames的步骤来打开文件并分配内存 提前感谢你的帮助。代码如下:使用fclose()时出现分段错误,c,segmentation-fault,C,Segmentation Fault,我是C语言的新手,我正在尝试将我的输出写入一个文件。当我试图关闭输出文件时,似乎遇到了分段错误。我按照sames的步骤来打开文件并分配内存 提前感谢你的帮助。代码如下: #include <stdio.h> #include <stdlib.h> #include <math.h> #define pi 4*atan(1) FILE *my_file_1; // Output file (*.txt) FILE *my_file_2; //
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define pi 4*atan(1)
FILE *my_file_1; // Output file (*.txt)
FILE *my_file_2; // Output file (*.txt)
int main()
{
int nx, ny, npts;
int niter;
double dt, w, height, d_o, h, factork, dVol;
double rho_o, g, lamda, po, co, p1, p2, r; // Fluid Properties
double *x, *y, *u, *v, *du, *dv, *rho, *drho, *p, *m;
double en_x, en_y, dw, dwx, dwy, sum1, sum2, sum3;
int i, j, k;
//Parameters
height = 0.2; //Height
w = 0.1; //width
nx = 20; //Number of particles x-dir
ny = 40; //Number of particles y-dir
npts = nx*ny;// Total number of particles
d_o = w / nx; // Distance between particles
dt = 0.00002;
dVol = 4 * pow((d_o / 2), 2);
// boundary
double L,height_wall,*xw1,*xw2,*xb,*yw1,*yw2,*yb;
int nyw1,nyw2,nxb;
int nrows,np1,np2,npb;
L=0.8;
height_wall=0.4;
nrows=3;
nyw1=height_wall/d_o;
nyw2=height_wall/d_o;
nxb=L/d_o;
np1=nrows*nyw1;
np2=nrows*nyw2;
npb=nrows*nxb;
printf("%d\n",npb);
// getchar();
h = 1.33*d_o; // Smoothing length
factork = 2; //Constant for kernell
//Fluid
rho_o = 1000;
g = 9.81;
lamda = 1;
po = 101325;
co = 30;
x = (double*)malloc(npts*sizeof(int));
y = (double*)malloc(npts*sizeof(int));
u = (double*)malloc(npts*sizeof(int));
v = (double*)malloc(npts*sizeof(int));
rho = (double*)malloc(npts*sizeof(int));
du = (double*)malloc(npts*sizeof(int));
dv = (double*)malloc(npts*sizeof(int));
drho = (double*)malloc(npts*sizeof(int));
p = (double*)malloc(npts*sizeof(int));
m = (double*)malloc(npts*sizeof(int));
//boundaries
xw1 = (double*)malloc(np1*sizeof(int));
yw1= (double*)malloc(np1*sizeof(int));
xw2 = (double*)malloc(np2*sizeof(int));
yw2= (double*)malloc(np2*sizeof(int));
xb = (double*)malloc(npb*sizeof(int));
yb= (double*)malloc(npb*sizeof(int));
my_file_1 = fopen("org.txt", "w");
my_file_2 = fopen("bound.txt", "w");
// Particles
for (i = 0; i<ny; i++)
{
for (j = 0; j<nx; j++)
{
x[nx*i + j] = d_o / 2 + j*d_o;
y[nx*(i)+j] = d_o / 2 + i*d_o;
// printf("%20.18f %20.18f\n",x[nx*i+j],y[nx*(i)+j]);
//printf("%7.4f %7.4f\n ", x[nx*i + j], y[nx*(i)+j]);
fprintf(my_file_1, "%7.4f %7.4f\n ", x[nx*i + j], y[nx*(i)+j]);
}
}
//Boundries
//Left wall
for (i = 0; i<nyw1; i++)
{
for (j = 0; j<nrows; j++)
{
xw1[nrows*i + j] = -L/2+d_o / 2 + j*d_o;
yw1[nrows*(i)+j] = d_o / 2 + i*d_o;
// printf("%20.18f %20.18f\n",x[nx*i+j],y[nx*(i)+j]);
//printf("%7.4f %7.4f\n ", x[nx*i + j], y[nx*(i)+j]);
fprintf(my_file_2, "%7.4f %7.4f\n ", xw1[nrows*i + j], yw1[nrows*(i)+j]);
}
}
//Right wall
for (i = 0; i<nyw2; i++)
{
for (j = 0; j<nrows; j++)
{
xw2[nrows*i + j] = L/2-4*d_o+3*d_o / 2 + j*d_o;
yw2[nrows*(i)+j] = d_o / 2 + i*d_o;
// printf("%20.18f %20.18f\n",x[nx*i+j],y[nx*(i)+j]);
//printf("%7.4f %7.4f\n ", x[nx*i + j], y[nx*(i)+j]);
fprintf(my_file_2, "%7.4f %7.4f\n ", xw2[nrows*i + j], yw2[nrows*(i)+j]);
}
}
//Bottom wall
for (i = 0; i<nrows; i++)
{
for (j = 0; j<nxb; j++)
{
xb[nxb*i + j] = -L/2+d_o/2 + j*d_o;
yb[nxb*i+j] = -4*d_o / 2 +i*d_o;
//printf("%d %d\n",i,j);
printf("%d %d\n",i,j);
printf("%20.18f %20.18f\n",xb[nxb*i+j],yb[nxb*(i)+j]);
//printf("%7.4f %7.4f\n ", x[nx*i + j], y[nx*(i)+j]);
fprintf(my_file_2, "%7.4f %7.4f\n ", xb[nxb*i + j], yb[nxb*(i)+j]);
}
}
fclose(my_file_1)
fclose(my_file_2)
}
#包括
#包括
#包括
#定义pi 4*atan(1)
文件*我的文件\u 1;//输出文件(*.txt)
文件*我的文件2;//输出文件(*.txt)
int main()
{
纽约州新南威尔士州国际机场;
内硝酸盐;
双dt,w,高度,d_o,h,系数,dVol;
双ρo,g,lamda,po,co,p1,p2,r;//流体性质
双*x,*y,*u,*v,*du,*dv,*rho,*drho,*p,*m;
双en_x,en_y,dw,dwx,dwy,sum1,sum2,sum3;
int i,j,k;
//参数
高度=0.2;//高度
w=0.1;//宽度
nx=20;//粒子数x-dir
ny=40;//粒子数y-dir
npts=nx*ny;//粒子总数
d_o=w/nx;//粒子之间的距离
dt=0.00002;
dVol=4*pow((d_o/2),2);
//边界
双L,墙高*xw1、*xw2、*xb、*yw1、*yw2、*yb;
int nyw1、nyw2、nxb;
国际净收益率、净现值1、净现值2、净现值b;
L=0.8;
墙的高度=0.4;
nrows=3;
nyw1=墙的高度/d_o;
nyw2=墙的高度/d\o;
nxb=L/d_o;
np1=nrows*nyw1;
np2=nrows*nyw2;
npb=nrows*nxb;
printf(“%d\n”,npb);
//getchar();
h=1.33*d_o;//平滑长度
factork=2;//kernell的常数
//流质
rho_o=1000;
g=9.81;
lamda=1;
po=101325;
co=30;
x=(双*)malloc(npts*sizeof(int));
y=(双*)malloc(npts*sizeof(int));
u=(双*)malloc(npts*sizeof(int));
v=(双*)malloc(npts*sizeof(int));
rho=(双*)malloc(npts*sizeof(int));
du=(双*)malloc(npts*sizeof(int));
dv=(双*)malloc(npts*sizeof(int));
drho=(双*)malloc(npts*sizeof(int));
p=(双*)malloc(npts*sizeof(int));
m=(双*)malloc(npts*sizeof(int));
//界限
xw1=(双*)malloc(np1*sizeof(int));
yw1=(双*)malloc(np1*sizeof(int));
xw2=(双*)malloc(np2*sizeof(int));
yw2=(双*)malloc(np2*sizeof(int));
xb=(双*)malloc(npb*sizeof(int));
yb=(双*)malloc(npb*sizeof(int));
my_file_1=fopen(“org.txt”,“w”);
my_file_2=fopen(“bound.txt”、“w”);
//粒子
对于(i=0;i,下面是对代码的返工,修复了内存分配以及阻止代码编译的bug!和大量样式更改。我还删除了所有不活动的代码,这在提交程序时是个好主意,这样人们就可以专注于问题。我还包装了你的fopen()
和fclose()
调用并进行错误检查,因此如果出现其他问题,您应该获得更多信息。下面的代码运行到完成时没有错误:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define pi (4 * atan(1))
#define ORG_FILE_NAME "org.txt"
#define BOUND_FILE_NAME "bound.txt"
int main()
{
// Parameters
double w = 0.1; // Width
int nx = 20; // Number of particles x-dir
int ny = 40; // Number of particles y-dir
int npts = nx * ny; // Total number of particles
double d_o = w / nx; // Distance between particles
// Boundary
double L = 0.8;
double height_wall = 0.4;
int nrows = 3;
int nyw1 = height_wall / d_o;
int nyw2 = height_wall / d_o;
int nxb = L / d_o;
int np1 = nrows * nyw1;
int np2 = nrows * nyw2;
int npb = nrows * nxb;
printf("%d\n", npb);
// Fluid
double *x = calloc(npts, sizeof(double));
double *y = calloc(npts, sizeof(double));
// Boundaries
double *xw1 = calloc(np1, sizeof(double));
double *yw1 = calloc(np1, sizeof(double));
double *xw2 = calloc(np2, sizeof(double));
double *yw2 = calloc(np2, sizeof(double));
double *xb = calloc(npb, sizeof(double));
double *yb = calloc(npb, sizeof(double));
FILE *my_file_1 = fopen(ORG_FILE_NAME, "w"); // Output file (*.txt)
if (my_file_1 == NULL)
{
perror(ORG_FILE_NAME);
return(EXIT_FAILURE);
}
FILE *my_file_2 = fopen(BOUND_FILE_NAME, "w"); // Output file (*.txt)
if (my_file_2 == NULL)
{
perror(BOUND_FILE_NAME);
return(EXIT_FAILURE);
}
// Particles
for (int i = 0; i < ny; i++)
{
for (int j = 0; j < nx; j++)
{
x[nx * i + j] = d_o / 2 + j * d_o;
y[nx * i + j] = d_o / 2 + i * d_o;
fprintf(my_file_1, "%7.4f %7.4f\n", x[nx * i + j], y[nx * i + j]);
}
}
// Boundries
// Left wall
for (int i = 0; i < nyw1; i++)
{
for (int j = 0; j < nrows; j++)
{
xw1[nrows * i + j] = -L / 2 + d_o / 2 + j * d_o;
yw1[nrows * i + j] = d_o / 2 + i * d_o;
fprintf(my_file_2, "%7.4f %7.4f\n", xw1[nrows * i + j], yw1[nrows * i + j]);
}
}
// Right wall
for (int i = 0; i < nyw2; i++)
{
for (int j = 0; j < nrows; j++)
{
xw2[nrows * i + j] = L / 2 - 4 * d_o + 3 * d_o / 2 + j * d_o;
yw2[nrows * i + j] = d_o / 2 + i * d_o;
fprintf(my_file_2, "%7.4f %7.4f\n", xw2[nrows * i + j], yw2[nrows * i + j]);
}
}
// Bottom wall
for (int i = 0; i < nrows; i++)
{
for (int j = 0; j < nxb; j++)
{
xb[nxb * i + j] = -L / 2 + d_o / 2 + j * d_o;
yb[nxb * i + j] = -4 * d_o / 2 + i * d_o;
fprintf(my_file_2, "%7.4f %7.4f\n", xb[nxb * i + j], yb[nxb * i + j]);
}
}
if (fclose(my_file_1) != 0)
{
perror(ORG_FILE_NAME);
return(EXIT_FAILURE);
}
if (fclose(my_file_2) != 0)
{
perror(BOUND_FILE_NAME);
return(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
#包括
#包括
#包括
#定义pi(4*atan(1))
#定义组织文件名“ORG.txt”
#定义绑定文件名“BOUND.txt”
int main()
{
//参数
双w=0.1;//宽度
int nx=20;//粒子数x-dir
int ny=40;//粒子数y-dir
int npts=nx*ny;//粒子总数
双d_o=w/nx;//粒子之间的距离
//边界
双L=0.8;
双高_墙=0.4;
int nrows=3;
int nyw1=墙的高度/d;
int nyw2=墙的高度/d;
int nxb=L/d_o;
int np1=nrows*nyw1;
int np2=nrows*nyw2;
int npb=nrows*nxb;
printf(“%d\n”,npb);
//流质
double*x=calloc(NPT,sizeof(double));
double*y=calloc(NPT,sizeof(double));
//界限
double*xw1=calloc(np1,sizeof(double));
double*yw1=calloc(np1,sizeof(double));
double*xw2=calloc(np2,sizeof(double));
double*yw2=calloc(np2,sizeof(double));
double*xb=calloc(npb,sizeof(double));
double*yb=calloc(npb,sizeof(double));
FILE*my_FILE_1=fopen(组织文件名,“w”);//输出文件(*.txt)
if(my_file_1==NULL)
{
perror(组织文件名);
返回(退出失败);
}
FILE*my_FILE_2=fopen(绑定文件名,“w”);//输出文件(*.txt)
if(my_file_2==NULL)
{
perror(绑定文件名);
返回(退出失败);
}
//粒子
对于(int i=0;i
为什么要铸造malloc并使用sizeof int作为初学者,x=(double*)malloc(npts*sizeof(int));
是一个很大的错误,而且