저는 C를 처음 사용하고 출력을 파일에 쓰려고합니다. 출력 파일을 닫으려고하면 세그먼트 화 오류가 발생하는 것 같습니다. 파일을 열고 메모리를 할당하는 몇 가지 예제로 sames 단계를 따랐습니다.fclose()를 사용할 때 분할 오류가 발생했습니다.
미리 도움을 주셔서 감사합니다.
다음#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)
}
이 우선 –
을 int로 왜 당신이 malloc을 캐스팅하고는 sizeof 사용,'X = (더블 *)의 malloc (npts *를 sizeof (INT)) : 아래의 코드는 오류없이 완료 될 때까지 실행 , 그리고 다른 15 개의 비슷한 줄. 코드를 한 줄씩 자세히 읽으시기 바랍니다. –
'fopen()'이 실패 할 수 있습니다 !!! 'fopen()'의 반환 값을 테스트하십시오. 만약 그것이'NULL'이라면 파일은 열려 있지 않습니다. http://www.cplusplus.com/reference/cstdio/fopen/ – francis