Like Tree3Likes
  • 1 Post By Subsonics
  • 1 Post By claudiu
  • 1 Post By claudiu

Segmentation fault ?

This is a discussion on Segmentation fault ? within the C Programming forums, part of the General Programming Boards category; Hey. I'm making a system to retrieve data from a .txt file, but I keep getting 'Segmentation fault.' The program ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    14

    Segmentation fault ?

    Hey. I'm making a system to retrieve data from a .txt file, but I keep getting 'Segmentation fault.' The program will compile but when I try to run it, it will crash. Here is the code, the problem being from fopen to fclose. I used this method before and it worked

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct{
            char fname[32];
            char lname[32];
            char address [64];
            char phone [16];
            char email [64];
            char credit_num[16];
            char credit_expiry[16];
            char credit_bsb[4];
            char licence_num[32];
            int ID;
            }customer;
    
    int main(){
        
        customer custData[128];
        int ch;
        char surname[32];
        char fname[32];
        FILE *cust;
        char address[64];
        char num[16];
        char mail[64];
        char crednum[16];
        char expiry[16];
        char bsb[4];
        char licnum[32];
        char line[128];
        char *item;
        int i = 0;
        int reccount = 1;
        int ID;
        
    
        
        cust = fopen("customer.txt", "r");                    //the problem is from somewhere here
        while(fgets(line,127,cust))
            {
                
                
                item = strtok(line, ";");
                
                strcpy(custData[reccount].fname, item);
                item = strtok(NULL, ";");
                
                strcpy(custData[reccount].lname, item);
                item = strtok(NULL, ";");
                
                strcpy(custData[reccount].address, item);
                item = strtok(NULL, ";");
                
                strcpy(custData[reccount].phone, item);
                item = strtok(NULL, ";");
            
                strcpy(custData[reccount].email, item);
                item = strtok(NULL, ";");
                
                strcpy(custData[reccount].credit_num, item);
                item = strtok(NULL, ";");
                
                strcpy(custData[reccount].credit_expiry, item);
                item = strtok(NULL, ";");
                
                strcpy(custData[reccount].credit_bsb, item);
                item = strtok(NULL, ";");
                
                strcpy(custData[reccount].licence_num, item);
                item = strtok(NULL, ";");
                
                custData[reccount].ID = atoi(item);
                
                reccount++;
                
       }
        fclose(cust);                            //to here
        
        printf("\nEnter customerID> ");
            scanf("%d", &ID);
            for(i=0;i<128;i++){
                    if(ID == custData[i].ID){
                           printf("First name> %s\nSurname> %s\nAddress> %s\nPhone Number> %s\nEmail Address> %s\nCredit Card Number> %s\nCredit Card Expiry> %s\nCredit Card BSB> %s\nLicence Number> %s\nCustomer ID> %d\n", custData[i].fname, custData[i].lname,custData[i].address,custData[i].phone,custData[i].email,custData[i].credit_num,custData[i].credit_expiry,custData[i].credit_bsb,custData[i].licence_num,custData[i].ID);
                                   }
                                   else
                                   {
                                   printf("CustomerID not found.");
                                                     }
                                           }
                   
        }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You have no error checking what so ever, this makes it impossible to fail gracefully or prevent the problem, instead you just crash. Some suspects are:

    • The file does not exist where you think it does.
    • strtok at one time returns NULL which makes strcpy crash
    • your file has more than 127 lines, or it has 127 but you start at index 1
    jigamuffin likes this.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    My suspicion is that your problem is the fact that you count your customer records from 1 rather than 0, and presumably you have 128 of them in the file, causing an overflow when reading the last customer data set.
    jigamuffin likes this.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Damnit Subsonics
    Subsonics likes this.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By sirsmilealot in forum C Programming
    Replies: 12
    Last Post: 02-10-2010, 12:26 PM
  2. why Segmentation fault?
    By zcrself in forum C Programming
    Replies: 12
    Last Post: 12-16-2009, 10:17 PM
  3. Segmentation fault
    By Kemaratu in forum C Programming
    Replies: 6
    Last Post: 11-30-2009, 02:40 AM
  4. segmentation fault
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 09-29-2005, 02:13 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

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