Segmentation fault when using fopen("filename","w+")
Hello,
Please help. I have no idea what's wrong with fopen with this time....
Code:
#include <stdio.h>#include <stdlib.h>
#include <math.h>
#define xfirst 0
#define xlast 300
#define tfirst 10
#define tlast 300
#define N 40
#define M 5
FILE *fpout;
main(){
/*FÜGGVÉNYEK*/
void forward(int number,double fwghost[], double dx,double *results[])
{
int i;
for(i=0;i<N;i++){
*results[i]=(fwghost[i+2]-fwghost[i+1])/dx;
}
}/*forward*/
void central(int number,double fwghost[], double dx,double *results[])
{
int i;
for(i=0;i<N;i++){
*results[i]=(fwghost[i+2]-fwghost[i])/(2*dx);
}
}/*central*/
double secondc(int number,double fwghost[], double dx,double *results[])
{
int i;
for(i=0;i<N;i++){
*results[i]=(fwghost[i+2]+2*fwghost[i+1]-fwghost[i])/(dx*dx);
}
}/*secondd*/
/*DEKLARÁCIÓK, ÉRTELMEZÉSI TARTOMÁNY*/
int i,j,k;
double t[M+2],x[N+2],u[N],u_anal[N],u_deriv[N],u_ghost[N+2];
double v=5.;
double dx=(xlast-xfirst)/(N-1);
double dt=(tlast-tfirst)/(M-1);
for(i=1;i<N+1;i++) x[i]=xfirst+dx*i;
x[0]=x[1]-dx;
x[N+1]=x[N]+dx;
for(i=1;i<M+1;i++) t[i]=tfirst+dt*i;
t[0]=t[1]-dt;
t[N+1]=t[N]+dt;
/*ANALITIKUS*/
for(i=0;i<M;i++){
for(j=0;j<N;j++){
u_anal[j]=1/(1+exp((2*x[j+1]-t[i])/(4*v)));
printf("%0.2lf %0.15lf\n",x[j+1],u_anal[j]);
}/*for j*/
}/*for i*/
printf("analitikus lefutott.");
for(j=0;j<N+1;j++){
u_ghost[j]=1/(1+exp((2*x[j]-t[0])/(4*v)));
}/*for j*/
printf("u_ghost kész.");
/*KIÍRATÁS*/
fpout=fopen("burgers_anal.dat","w+");
for(j=0;j<N;j++){
fprintf(fpout,"%0.2lf %0.15lf\n",x[j+1],u_anal[j]);
}/*for j*/
//fclose(fpout);
fflush(fpout);
}
I get segmentation fault when i try to write the data in file. I have never had this kind of problem. I read a few posts about this kind of seg. f., but I still don't get what's the problem with my code.