gpt4 book ai didi

c - 使用 fclose() 时出现段错误

转载 作者:行者123 更新时间:2023-11-30 18:50:33 26 4
gpt4 key购买 nike

我是 C 新手,我正在尝试将输出写入文件。当我尝试关闭输出文件时,似乎遇到了段错误。我按照与一些示例相同的步骤打开文件并分配内存。

预先感谢您的帮助。这是代码:

#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)
}

最佳答案

下面是对代码的修改,修复了内存分配,并修复了导致代码无法编译的错误!并且风格发生了很多变化。我还删除了所有不活动的代码,这是提交程序时的一个好主意,以便人们可以专注于问题。我还对您的 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;
}

关于c - 使用 fclose() 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39936149/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com