Like Tree1Likes
  • 1 Post By JohnGraham

Segmentation fault when using fopen("filename","w+")

This is a discussion on Segmentation fault when using fopen("filename","w+") within the C Programming forums, part of the General Programming Boards category; 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> ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    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.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    A segfault can occur because of data corruption from anywhere in a program, and can be caused by data corruption that occurred earlier in the program. In your case, the line:

    Code:
    t[N + 1] = t[N] + dt;
    is giving you the trouble, because t[] has a dimension of 7 (M+2) but you're accessing indexes N and N+1 (40 and 41).
    rags_to_riches likes this.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Line 1 .. you have 2 preprocessor statments on the same line... C doesn't know how to do that

    Line 11... fpout is declared at global scope but only used in main...

    Line 14... unless this is for a microcontroller, the OS will expect an integer return value from main()... The correct form is...
    Code:
    int main (void)
      {
         // your code goes here
    
         return 0; }
    This matters because even if your program appears to work correctly it can cause problems for the OS itself since scripts, batch files and parent programs often rely upon the return value from main() to determine success or failure.


    Lines 18 to 42 ... You are declaring functions inside your main... C does not support "nested" functions...

    Line 82... you did not check to see if the file actually opens but proceeded to write to it anyway.

    Line 86 and 87 ... you should not be flushing a file after closing it... and you shouldn't do either if you're not certain it actually opened.


    You seriously need to learn how to indent code according to some rational scheme... Standards.. HERE
    Last edited by CommonTater; 11-20-2011 at 06:22 PM.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    Thank both of you!

    I haven't realized this
    Code:
    t[N + 1] = t[N] + dt;
    because the code was running without the fopen().
    Thanks for the advices.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Marslakoo View Post
    because the code was running without the fopen().
    Thanks for the advices.
    Your code has never worked...
    Here are the error messages I get from Pelles C (C-99 standard)... Pay special attention to the last 3 lines...
    Code:
    Building main.obj.
    D:\Programming\C_Code\Experiments\junk\main.c(1): error #1036: Syntax error in #include.
    D:\Programming\C_Code\Experiments\junk\main.c(11): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(11): error #2001: Syntax error: expected ';' but found '*'.
    D:\Programming\C_Code\Experiments\junk\main.c(11): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(14): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(14): warning #2117: Old-style function definition for 'main'.
    D:\Programming\C_Code\Experiments\junk\main.c(19): error #2001: Syntax error: expected ';' but found '{'.
    D:\Programming\C_Code\Experiments\junk\main.c(22): error #2048: Undeclared identifier 'results'.
    D:\Programming\C_Code\Experiments\junk\main.c(22): error #2144: Type error: pointer expected.
    D:\Programming\C_Code\Experiments\junk\main.c(22): error #2144: Type error: pointer expected.
    D:\Programming\C_Code\Experiments\junk\main.c(22): error #2048: Undeclared identifier 'fwghost'.
    D:\Programming\C_Code\Experiments\junk\main.c(22): error #2144: Type error: pointer expected.
    D:\Programming\C_Code\Experiments\junk\main.c(22): error #2144: Type error: pointer expected.
    D:\Programming\C_Code\Experiments\junk\main.c(22): error #2048: Undeclared identifier 'dx'.
    D:\Programming\C_Code\Experiments\junk\main.c(27): warning #2118: Parameter 'number' is not referenced.
    D:\Programming\C_Code\Experiments\junk\main.c(36): warning #2118: Parameter 'number' is not referenced.
    D:\Programming\C_Code\Experiments\junk\main.c(55): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(55): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(55): error #2001: Syntax error: expected ')' but found '='.
    D:\Programming\C_Code\Experiments\junk\main.c(55): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(55): error #2001: Syntax error: expected ';' but found '<'.
    D:\Programming\C_Code\Experiments\junk\main.c(55): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(55): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(55): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(55): warning #2014: Empty declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(55): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(55): error #2001: Syntax error: expected ';' but found '++'.
    D:\Programming\C_Code\Experiments\junk\main.c(55): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(55): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(55): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(55): error #2111: Integer expression must be constant.
    D:\Programming\C_Code\Experiments\junk\main.c(55): error #2120: Redeclaration of 'x', previously declared at D:\Programming\C_Code\Experiments\junk\main.c(47); expected 'double [42]' but found 'int [1]'.
    D:\Programming\C_Code\Experiments\junk\main.c(55): error #2098: Missing '{' in initialization of 'int [1]'.
    D:\Programming\C_Code\Experiments\junk\main.c(55): warning #2215: Conversion from 'double' to 'int'; possible loss of data.
    D:\Programming\C_Code\Experiments\junk\main.c(55): error #2069: Initializer must be constant.
    D:\Programming\C_Code\Experiments\junk\main.c(56): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(56): error #2123: Redefinition of 'x', previously defined at D:\Programming\C_Code\Experiments\junk\main.c(55).
    D:\Programming\C_Code\Experiments\junk\main.c(56): error #2098: Missing '{' in initialization of 'int [1]'.
    D:\Programming\C_Code\Experiments\junk\main.c(56): warning #2215: Conversion from 'double' to 'int'; possible loss of data.
    D:\Programming\C_Code\Experiments\junk\main.c(56): error #2069: Initializer must be constant.
    D:\Programming\C_Code\Experiments\junk\main.c(57): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(57): error #2120: Redeclaration of 'x', previously declared at D:\Programming\C_Code\Experiments\junk\main.c(56); expected 'int [1]' but found 'int [41]'.
    D:\Programming\C_Code\Experiments\junk\main.c(57): error #2123: Redefinition of 'x', previously defined at D:\Programming\C_Code\Experiments\junk\main.c(56).
    D:\Programming\C_Code\Experiments\junk\main.c(57): error #2098: Missing '{' in initialization of 'int [41]'.
    D:\Programming\C_Code\Experiments\junk\main.c(57): warning #2215: Conversion from 'double' to 'int'; possible loss of data.
    D:\Programming\C_Code\Experiments\junk\main.c(57): error #2069: Initializer must be constant.
    D:\Programming\C_Code\Experiments\junk\main.c(58): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(58): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(58): error #2001: Syntax error: expected ')' but found '='.
    D:\Programming\C_Code\Experiments\junk\main.c(58): error #2123: Redefinition of 'i', previously defined at D:\Programming\C_Code\Experiments\junk\main.c(55).
    D:\Programming\C_Code\Experiments\junk\main.c(58): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(58): error #2001: Syntax error: expected ';' but found '<'.
    D:\Programming\C_Code\Experiments\junk\main.c(58): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(58): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(58): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(58): warning #2014: Empty declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(58): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(58): error #2001: Syntax error: expected ';' but found '++'.
    D:\Programming\C_Code\Experiments\junk\main.c(58): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(58): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(58): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(58): error #2111: Integer expression must be constant.
    D:\Programming\C_Code\Experiments\junk\main.c(58): error #2120: Redeclaration of 't', previously declared at D:\Programming\C_Code\Experiments\junk\main.c(47); expected 'double [7]' but found 'int [1]'.
    D:\Programming\C_Code\Experiments\junk\main.c(58): error #2098: Missing '{' in initialization of 'int [1]'.
    D:\Programming\C_Code\Experiments\junk\main.c(58): warning #2215: Conversion from 'double' to 'int'; possible loss of data.
    D:\Programming\C_Code\Experiments\junk\main.c(58): error #2069: Initializer must be constant.
    D:\Programming\C_Code\Experiments\junk\main.c(59): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(59): error #2123: Redefinition of 't', previously defined at D:\Programming\C_Code\Experiments\junk\main.c(58).
    D:\Programming\C_Code\Experiments\junk\main.c(59): error #2098: Missing '{' in initialization of 'int [1]'.
    D:\Programming\C_Code\Experiments\junk\main.c(59): warning #2215: Conversion from 'double' to 'int'; possible loss of data.
    D:\Programming\C_Code\Experiments\junk\main.c(59): error #2069: Initializer must be constant.
    D:\Programming\C_Code\Experiments\junk\main.c(60): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(60): error #2120: Redeclaration of 't', previously declared at D:\Programming\C_Code\Experiments\junk\main.c(59); expected 'int [1]' but found 'int [41]'.
    D:\Programming\C_Code\Experiments\junk\main.c(60): error #2123: Redefinition of 't', previously defined at D:\Programming\C_Code\Experiments\junk\main.c(59).
    D:\Programming\C_Code\Experiments\junk\main.c(60): error #2098: Missing '{' in initialization of 'int [41]'.
    D:\Programming\C_Code\Experiments\junk\main.c(60): warning #2215: Conversion from 'double' to 'int'; possible loss of data.
    D:\Programming\C_Code\Experiments\junk\main.c(60): error #2069: Initializer must be constant.
    D:\Programming\C_Code\Experiments\junk\main.c(64): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(64): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(64): error #2001: Syntax error: expected ')' but found '='.
    D:\Programming\C_Code\Experiments\junk\main.c(64): error #2123: Redefinition of 'i', previously defined at D:\Programming\C_Code\Experiments\junk\main.c(58).
    D:\Programming\C_Code\Experiments\junk\main.c(64): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(64): error #2001: Syntax error: expected ';' but found '<'.
    D:\Programming\C_Code\Experiments\junk\main.c(64): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(64): warning #2014: Empty declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(64): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(64): error #2001: Syntax error: expected ';' but found '++'.
    D:\Programming\C_Code\Experiments\junk\main.c(64): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(64): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(64): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(65): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(65): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(65): error #2001: Syntax error: expected ')' but found '='.
    D:\Programming\C_Code\Experiments\junk\main.c(65): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(65): error #2001: Syntax error: expected ';' but found '<'.
    D:\Programming\C_Code\Experiments\junk\main.c(65): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(65): warning #2014: Empty declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(65): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(65): error #2001: Syntax error: expected ';' but found '++'.
    D:\Programming\C_Code\Experiments\junk\main.c(65): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(65): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(65): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(66): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(66): error #2111: Integer expression must be constant.
    D:\Programming\C_Code\Experiments\junk\main.c(66): error #2120: Redeclaration of 'u_anal', previously declared at D:\Programming\C_Code\Experiments\junk\main.c(47); expected 'double [40]' but found 'int [1]'.
    D:\Programming\C_Code\Experiments\junk\main.c(66): error #2098: Missing '{' in initialization of 'int [1]'.
    D:\Programming\C_Code\Experiments\junk\main.c(66): warning #2215: Conversion from 'double' to 'int'; possible loss of data.
    D:\Programming\C_Code\Experiments\junk\main.c(66): error #2069: Initializer must be constant.
    D:\Programming\C_Code\Experiments\junk\main.c(67): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(67): error #2001: Syntax error: expected ')' but found 'string constant'.
    D:\Programming\C_Code\Experiments\junk\main.c(67): warning #2028: Missing prototype.
    D:\Programming\C_Code\Experiments\junk\main.c(68): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(69): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(70): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(70): error #2001: Syntax error: expected ')' but found 'string constant'.
    D:\Programming\C_Code\Experiments\junk\main.c(70): warning #2028: Missing prototype.
    D:\Programming\C_Code\Experiments\junk\main.c(73): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(73): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(73): error #2001: Syntax error: expected ')' but found '='.
    D:\Programming\C_Code\Experiments\junk\main.c(73): error #2123: Redefinition of 'j', previously defined at D:\Programming\C_Code\Experiments\junk\main.c(65).
    D:\Programming\C_Code\Experiments\junk\main.c(73): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(73): error #2001: Syntax error: expected ';' but found '<'.
    D:\Programming\C_Code\Experiments\junk\main.c(73): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(73): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(73): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(73): warning #2014: Empty declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(73): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(73): error #2001: Syntax error: expected ';' but found '++'.
    D:\Programming\C_Code\Experiments\junk\main.c(73): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(73): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(73): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(74): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(74): error #2111: Integer expression must be constant.
    D:\Programming\C_Code\Experiments\junk\main.c(74): error #2120: Redeclaration of 'u_ghost', previously declared at D:\Programming\C_Code\Experiments\junk\main.c(47); expected 'double [42]' but found 'int [1]'.
    D:\Programming\C_Code\Experiments\junk\main.c(74): error #2098: Missing '{' in initialization of 'int [1]'.
    D:\Programming\C_Code\Experiments\junk\main.c(74): warning #2215: Conversion from 'double' to 'int'; possible loss of data.
    D:\Programming\C_Code\Experiments\junk\main.c(74): error #2069: Initializer must be constant.
    D:\Programming\C_Code\Experiments\junk\main.c(75): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(78): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(78): error #2001: Syntax error: expected ')' but found 'string constant'.
    D:\Programming\C_Code\Experiments\junk\main.c(78): warning #2028: Missing prototype.
    D:\Programming\C_Code\Experiments\junk\main.c(82): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(82): error #2120: Redeclaration of 'fpout', previously declared at D:\Programming\C_Code\Experiments\junk\main.c(11); expected 'int *' but found 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(82): warning #2027: Missing prototype for 'fopen'.
    D:\Programming\C_Code\Experiments\junk\main.c(82): error #2069: Initializer must be constant.
    D:\Programming\C_Code\Experiments\junk\main.c(83): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(83): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(83): error #2001: Syntax error: expected ')' but found '='.
    D:\Programming\C_Code\Experiments\junk\main.c(83): error #2123: Redefinition of 'j', previously defined at D:\Programming\C_Code\Experiments\junk\main.c(73).
    D:\Programming\C_Code\Experiments\junk\main.c(83): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(83): error #2001: Syntax error: expected ';' but found '<'.
    D:\Programming\C_Code\Experiments\junk\main.c(83): error #2156: Unrecognized declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(83): warning #2014: Empty declaration.
    D:\Programming\C_Code\Experiments\junk\main.c(83): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(83): error #2001: Syntax error: expected ';' but found '++'.
    D:\Programming\C_Code\Experiments\junk\main.c(83): fatal error #2210: More than 100 errors, please improve yourself.
    If you are compiling a Windows program, make sure you use the /Ze option!
    *** Error code: 1 ***

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by CommonTater View Post
    Your code has never worked...
    His code certainly has worked - all the errors you see are caused by the double-#include on the first line, which is probably a simple cut & paste error. Fix that and I just get from gcc is a few warnings about unused variables and no return value.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by JohnGraham View Post
    His code certainly has worked - all the errors you see are caused by the double-#include on the first line, which is probably a simple cut & paste error. Fix that and I just get from gcc is a few warnings about unused variables and no return value.
    Well... that and functions defined inside main(), array bounds errors, etc...

    Still gives that classic Pelles C error message: "More than 100 errors, please improve yourself."

    However... if I change it like this...
    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
    
    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;
          }
      }
    
    
    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);
        }
      }
    
    
    void 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);
          }
      }
    
    
    
    int main( void )
      {
        int i,j;
        double t[N+2],x[N+2],u_anal[N],u_ghost[N+2];
        double v=5.;
        FILE *fpout;
    
        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;
    
        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]);
              }
          }
    
        printf("analitikus lefutott.\n");
    
    
        for(j=0;j<N+1;j++)
          {
            u_ghost[j]=1/(1+exp((2*x[j]-t[0])/(4*v)));
          }
    
    
        printf("u_ghost kész.\n");
    
        fpout=fopen("burgers_anal.dat","w+");
        if ( ! fpout)
          { 
            printf("Cannot write file\n\n");
            return 1;
          }
    
        for(j=0;j<N;j++)
          {
            fprintf(fpout,"%0.2lf %0.15lf\n",x[j+1],u_anal[j]);
          }
    
        fflush(fpout);
        fclose(fpout);
        return 0;
    }
    ... it does work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nbin=fopen("input.txt","a"); doesn't work?
    By Adam Rinkleff in forum C Programming
    Replies: 2
    Last Post: 06-23-2011, 03:57 PM
  2. [Segmentation Fault] fopen("filename","w")
    By gibbofresco in forum C Programming
    Replies: 7
    Last Post: 07-04-2009, 05:32 AM
  3. Segmentation fault by fopen(file,"r")
    By Stefan in forum C Programming
    Replies: 3
    Last Post: 02-12-2006, 04:37 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21