wierd segmentation fault with fopen

This is a discussion on wierd segmentation fault with fopen within the C Programming forums, part of the General Programming Boards category; So, I use the following code with a pre-existing txt file. It works perfectly fine on my MAc laptop and ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    3

    wierd segmentation fault with fopen

    So, I use the following code with a pre-existing txt file. It works perfectly fine on my MAc laptop and segmentation faults on my linux machine. Any suggestions would be appreciated. I basically need to create a file in IDL then read it into C so any methodology will work.

    Thanks,
    Jared

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    FILE *in_file, *out_file;
    /*out_file should be an axact duplicate of in_file (the file created in IDL)*/
    
    /*program to open a text file from idl and read it into c*/
    int main()
    {
       char temp[80];        /*temporary string to store line from file in */
       int values;         /*value of line, will be velocity or density */
       int i,j,ii,jj;              /*keep grid point data and use as a check later*/
       int max_i=511, max_j=511; /*control variables for file i/o, set by nput grid size*/
       int done;             /*used to close file*/
       int spectrum[max_i][max_j];   /*stores data*/
       /*open idl file*/
       in_file=fopen("cloud.txt","r");
       if(in_file == NULL)
       {
          printf("Failed to open in file!\n");
              exit(-1);
            }
    
       /*open duplicate file*/
       out_file=fopen("c.txt", "w");
       if(out_file == NULL)
       {
          printf("Failed to open out file!\n");
              exit(-1);
            }
    
            /*Now read from infile and write to out file*/
        for (ii = 0; ii<max_i+1; ++ii)
            {
               for (jj = 0; jj<max_j+1; ++jj)
               {
                  fgets(temp, sizeof(temp), in_file);
                  sscanf(temp, "%d", &values);
                  fputs(temp, out_file);
                      spectrum[ii][jj]=values;
                    /*  printf("%d %d\n",ii,jj);*/
               }
            }
            done = close(out_file);
            done = close(in_file);
       return(0);
    }

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    Indicating which line is causing the segfault would be helpful...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Change

    Code:
    for (ii = 0; ii<max_i+1; ++ii)
            {
               for (jj = 0; jj<max_j+1; ++jj)
    to

    Code:
    for (ii = 0; ii<max_i; ++ii)
            {
               for (jj = 0; jj<max_j; ++jj)

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    3
    That fixed it but there must be something in the larger body of code giving me a segmentation fault. How do I debug where it is coming from if I am using cc as my compiler?

    I am reading a txt file into a 512x512 array. My computational grid is 1024x512x1 and I am trying to stick the array I read into it on the right hand side. I've highlighted where I think the segmentation fault is still occuring. xrho and yrho are 512 and 512 respectively.

    Code:
     /*READ IN THE VALUES GENERATED BY THE FOURIER TRANSFORM*/
      /********************************************************************************/
       FILE *in_file;
       char temp[80];        /*temporary string to store line from file in */
       int values;         /*value of line, will be velocity or density */
       int ii,jj;              /*keep grid point data and use as a check later*/
       int done;             /*used to close file*/
       Real cloud[xrho][yrho];   /*stores data*/
       /*open idl file*/
    
       in_file=fopen("cloud.txt","r");
       if(in_file == NULL)
       {
          printf("Failed to open in file!\n");
              exit(-1);
            }
    
    
            /*Now read from infile and write tocloud array*/
        for (ii = 0; ii<xrho; ++ii)
            {
               for (jj = 0; jj<yrho; ++jj)
               {
                  fgets(temp, sizeof(temp), in_file);
                  sscanf(temp, "&#37;f", &values);
                      cloud[ii][jj]=values;
               }
            }
            done = close(in_file);
      /********************************************************************************/
    
      /*INITIALIZE VALUES HERE*/
      /*FIRST JUST PUT A CLOUD ON THE RIGHT HAND SIDE, WE"LL LOAD THE REAL STUF TOMORROW*/
      for (k=ks; k<=ke; k++) {
        for (j=js; j<=je; j++) {
          for (i=is; i<=ie; i++) {
            /*GET THE POSITION EVERYWHERE*/
            cc_pos(pGrid,i,j,k,&x1,&x2,&x3);
            /*NO FLOW IN Y OR Z*/
            pGrid->U[k][j][i].M2 =0.0;
            pGrid->U[k][j][i].M3 =0.0;
            /*SET THE DENSITY*/
            if (i >= xrho){
      pGrid->U[k][j][i].d = cloud[i-xrho][j];
            dd=sqrt((x1-lx/4.)*(x1-lx/4.)+x2*x2+x3*x3);
            exponent = pow(dd/1.5,2);
            pGrid->U[k][j][i].d *=exp(-exponent);
            pGrid->U[k][j][i].M1=velocity*pGrid->U[k][j][i].d;
            }
            if (i < xrho) {
            pGrid->U[k][j][i].d=1e-5;
            pGrid->U[k][j][i].M1=0.0;
            }
    Last edited by jaredw; 08-27-2008 at 07:34 PM.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    3
    I am in fact, positive that it is that line. The code as posted above works if I comment it out and change the *= to = in the line setting P->U[k][j][i].d

    Could someone also explain why it worked on a mac and not a linux?

    Thanks for the help. I'm just learning c.

    It looks like I'm not reading into my cloud array what I think I am and that might be causing the seg fault when I try to use the value.
    Last edited by jaredw; 08-27-2008 at 07:16 PM.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah... I have had that happen when writing multi-platform code before. 9 times out of 10 its because linux actually closely watches what errors your program may be generating and kills it when it does something wrong. Some OSes are more forgiving to the programmer than the user. Personally, I would rather be punished, not my clients.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault after fopen
    By dumpa in forum C Programming
    Replies: 7
    Last Post: 08-06-2008, 10:21 AM
  2. fopen segmentation fault
    By fguy817817 in forum C Programming
    Replies: 27
    Last Post: 06-04-2008, 12:17 PM
  3. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 04:50 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

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