Thread: hi giving core dump

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    26

    hi giving core dump

    Code:
    #include <stdio.h>
    #include <strings.h>
    #include <stdlib.h>
    
    typedef struct 
    {
            long int flight_no;
    
            float   Fare,
                    Carry_cost_1,   
                    Carry_cost_2,   
                    Carry_cost_3;   
    
            char    orig[5],
                    Dest[5];
    } Flight;
    
    int main ()
    {
    
            FILE *fp;
    
            char c[100],*token;
    
            int i,line;
    
            Flight f1;
    
            fp=fopen("filght.txt","rb");
            
            if (fp == NULL)
            {
                    printf("Error: can't open the file filght.txt:");
            }
    
            line=1;
    
            while (fgets(c,100,fp)!=NULL)
            {
                    printf("The %d line is as follow:\n",line);
                    puts(c);
    
                    if ((token = strtok(c, "|")) != NULL)
                    {
                            i=1;
    
                            f1.flight_no=atol(token);
                            printf("Filght number:%ld \n",f1.flight_no);
                            while ((token = strtok(NULL, "|")) != NULL) 
                            {
                                    i++;
                                    if (i==2)
                                    {
                                            strcpy(f1.orig,token);
                                            printf("Source = \"%s\"\n",f1.orig);
                                    }
                                    if (i==3)
                                    {
                                            strcpy(f1.Dest,token);
                                            printf("Dest= \"%s\"\n",f1.Dest);
                                    }
                                    if (i==4)
                                    {
                                            f1.Fare=atof(token);
                                            printf("Fare = \"%.3f\"\n",f1.Fare);
                                    }
                                    if (i==5)
                                    {
                                            f1.Carry_cost_1=atof(token);
                                            printf("Carry_cost_1= \"%.3f\"\n",f1.Carry_cost_1);
                                    }
                                    if (i==6)
                                    {
                                            f1.Carry_cost_2=atof(token);
                                            printf("Carry_cost_2 = \"%.3f\"\n",f1.Carry_cost_2);
                                    }
                                    if (i==7)
                                    {
                                            f1.Carry_cost_3=atof(token);
                                            printf("Carry_cost_3 = \"%.3f\"\n",f1.Carry_cost_3);
                                    }
                            }
                    }
                    line++;
                    printf("Total_carry_cost =:%.3f\n",f1.Carry_cost_1+f1.Carry_cost_2+f1.Carry_cost_3);
            }
    
            fclose (fp);
    
            return 0;
    }
    o/p i am getting is
    ./program2
    Segmentation fault(coredump)
    plese suggest me

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you put a newline on this line, does it appear on your screen:
    Code:
                    printf("Error: can't open the file filght.txt:");
    I suspect your spelling of "flight" doesn't match the actual file-name.

    When you have an error opeing a file, it's a good idea to stop there, not continue using the file that you couldn't open... So calling exit(1) or some such in the if-statement that prints the error...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    Thanks for ur valuable reply i got it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hi to over come with core dump
    By vijay85 in forum C Programming
    Replies: 9
    Last Post: 01-14-2009, 11:59 AM
  2. having a core dump
    By justins in forum C Programming
    Replies: 6
    Last Post: 05-21-2008, 12:00 PM
  3. STL vector core dump
    By creativeinspira in forum C++ Programming
    Replies: 9
    Last Post: 07-22-2007, 04:49 PM
  4. Core Dump in While()
    By KrepNatas in forum C Programming
    Replies: 5
    Last Post: 05-17-2005, 11:15 AM
  5. core dump
    By kermit in forum Linux Programming
    Replies: 0
    Last Post: 08-03-2004, 06:25 PM