2016-10-08 5 views
-1

저는 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) 
} 
+3

이 우선 –

+4

을 int로 왜 당신이 malloc을 캐스팅하고는 sizeof 사용,'X = (더블 *)의 malloc (npts *를 sizeof (INT)) : 아래의 코드는 오류없이 완료 될 때까지 실행 , 그리고 다른 15 개의 비슷한 줄. 코드를 한 줄씩 자세히 읽으시기 바랍니다. –

+3

'fopen()'이 실패 할 수 있습니다 !!! 'fopen()'의 반환 값을 테스트하십시오. 만약 그것이'NULL'이라면 파일은 열려 있지 않습니다. http://www.cplusplus.com/reference/cstdio/fopen/ – francis

답변

2

컴파일에서 코드를 유지 버그 수정뿐만 아니라 고정 메모리 할당과 코드의 재 작업입니다 :이 코드입니다! 스타일이 많이 바뀝니다. 나는 SO 사람들이 문제에 집중할 수 있도록 프로그램을 제출할 때 좋은 아이디어 인 모든 비활성 코드를 제거했습니다. 또한 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; 
} 
+0

예. 마지막으로, 누군가가'fclose()'의 반환 값을 검사하는 예제를 본다. –

+0

도움 주셔서 감사합니다. – user6942453

관련 문제