Find the answer to your Linux question:
Results 1 to 3 of 3
Hi everyone. I will appreciate your help. I moved some C programs to other computer, from HPUX to Redhat Linux. All of my programs worked perfectly, but one is giving ...
  1. #1
    Just Joined!
    Join Date
    May 2009
    Posts
    1

    Question "Segmentation fault" error, C programmin on Linux

    Hi everyone.

    I will appreciate your help.

    I moved some C programs to other computer, from HPUX to Redhat Linux.

    All of my programs worked perfectly, but one is giving me problems.

    When I compile the C file I get these warnings:

    Code:
    # cc -o tgmeas_tgcomp tgmeas_tgcomp.c
    tgmeas_tgcomp.c: In function at main:
    tgmeas_tgcomp.c:43: warning: assignment makes integer from pointer without a cast
    And when I run the executable file (tgmeas_tgcomp) I get this:
    Segmentation fault

    I tried to compile it using gcc, but I get the same result.

    I changed the code declaring "main" as "main(void)", but it still not working.

    Here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <strings.h>
    
    FILE *fp1,*fp2,*fp3,*fp4,*fp5,*fp6,*fp7,*fp8,*fp9;
    char c1[120],c2[120],cp[120],*aux=0;
    int troncal,tgn,inserv,outpc,opc,outans,totusg,mtusg,incpc,incans,avgoos,insvc;
    int TOT;
    float CAPINSVC,OCC,ABR,ASR,HT,IOR;
    
    main()
    {
      if((fp1=fopen("TGMEAS/tgmeas.datos","r"))==NULL)
      {
        exit(1);
      }
    
      fp2=fopen("TGMEAS/trunks.ok","r");
    
      /*if((fp2=fopen("TGMEAS/trunks.ok","r"))==NULL)
      { 
        exit(1);
      }*/
     
      fp3=fopen("TGMEAS/tgmeas.5ess","w");
      while(!feof(fp1))
      {
         fgets(c1,sizeof(c1),fp1);
         strcpy(cp,c1);
         aux=strtok(cp,"\t");
         tgn=atoi(aux);
         while(!feof(fp2))
         {
            fgets(c2,sizeof(c2),fp2);
            strcpy(cp,c2);
            aux=strtok(cp,"\t");
            troncal=atoi(aux);
            aux=strtok('\0',"\t");
            inserv=atoi(aux);
            if(tgn == troncal)
            {
               c1[strlen(c1)-1]=NULL;
               if(!feof(fp1) && !feof(fp2))
                 fprintf(fp3,"%s \t%d\n",c1,inserv);
               break;
            }
         }
         rewind(fp2);
      }
      fclose(fp1);
      fclose(fp2);
      fclose(fp3);
    
      system("/home/iw6/TGMEAS/.tgmeas.5ess > /home/iw6/TGMEAS/tgmeas.insvc");
      
      if((fp4=fopen("TGMEAS/tgmeas.insvc","r"))==NULL)
      {
        exit(1);
      }
      fp5=fopen("NETWORK/oos.txt","w");
      fp6=fopen("NETWORK/abr.txt","w");
      fp7=fopen("NETWORK/asr.txt","w");
      fp8=fopen("NETWORK/htime.txt","w");
      fp9=fopen("NETWORK/ior.txt","w");
      
      while(!feof(fp4))
      {
         fgets(c2,sizeof(c2),fp4);
         strcpy(cp,c2);
         aux=strtok(cp," ");
         tgn=atoi(aux);
         aux=strtok('\0'," ");
         outpc=atoi(aux);
         aux=strtok('\0'," ");
         outans=atoi(aux);
         aux=strtok('\0'," ");
         aux=strtok('\0'," ");
         totusg=atoi(aux);
         aux=strtok('\0'," ");
         mtusg=atoi(aux);
         aux=strtok('\0'," ");
         incpc=atoi(aux);
         aux=strtok('\0'," ");
         incans=atoi(aux);
         aux=strtok('\0'," ");
         avgoos=atoi(aux);
         aux=strtok('\0'," ");
         insvc=atoi(aux);
         
         /*printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",tgn,outpc,outans,totusg,mtusg,incpc,incans,avgoos,insvc);*/
         
    
                     /********* % OCUPANCY BY TRUNK GROUP ********/
    
         TOT=avgoos+insvc;
         if(TOT != 0)
         {
           CAPINSVC=((float)insvc/(float)TOT)*100;
           OCC=((((float)totusg-(float)mtusg)/100)/(float)TOT)*100;
         }
         else
         {
            CAPINSVC=OCC=0.00;
         }
           fprintf(fp5,"%d\t100\t%3.2f\t%3.2f\n",tgn,CAPINSVC,OCC);
         
                             
                             /********* % ABR ********/
                    
         if(incpc != 0)
         {
           ABR=((float)incans/(float)incpc)*100;
         }
         else
         {
           ABR=0.0;
         }
           fprintf(fp6,"%d\t%d\t%d\t%3.2f\n",tgn,incpc,incans,ABR);
         
    
                             /********* % ASR ********/
                    
         if(outpc != 0)
         {
           ASR=((float)outans/(float)outpc)*100;
         }
         else
         {
           ASR=0.0;
         }
         if(!feof(fp4))
           fprintf(fp7,"%d\t%d\t%d\t%3.2f\n",tgn,outpc,outans,ASR);
    
    
                          /********* % HOLDING TIME ********/
                    
         if(incpc == 0 && outpc == 0)
         {
            HT=0.0;
         }
         else
         {
            HT=(((((float)totusg-(float)mtusg)/100)*3)/((float)incpc+(float)outpc))/(0.6);
         }
           fprintf(fp8,"%d\t%d\t%d\t%3.2f\n",tgn,outpc,incpc,HT);
    
                            
                             /********* % IOR ********/
                    
         if(outpc != 0 || incpc != 0)
         {
           IOR=((float)outpc/((float)incpc+(float)outpc))*100;
         }
         else
         {
           IOR=0.0;
         }
           fprintf(fp9,"%d\t%d\t%d\t100\t%3.2f\n",tgn,outpc,incpc,IOR);
    
      }  
      fclose(fp4);
      fclose(fp5);  
      fclose(fp6);
      fclose(fp7);  
      fclose(fp8);
      fclose(fp9);
    }

  2. #2
    Just Joined!
    Join Date
    May 2009
    Posts
    6
    I think you are getting the warning because of the value of the NULL macro in stddef.h and how you are using it in this line:
    Code:
    c1[strlen(c1)-1]=NULL;
    However, I think this has nothing to do with your seg fault. Allow your env to create a core file and then look at the stack trace by using gdb.

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Are you sure that all the files were opened successfully? You don't test the resulting fpN variables fp5 thru fp9, yet you write to them. If any did not open, you are toast.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...