Thread: Segmentation Error

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    12

    Segmentation Error

    For some reason I keep on getting a segmentation error when trying to run the code.

    Does anyone know how I can fix it?:

    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAXLEN     120 
    
    
    void checkin()
    {
       typedef struct {
            char reservationId[7];
            char customerId[7];
            char roomId[5];
            int numGuests;
            char startDate[10];
            char endDate[10];
            char status[10];	
    
    
                   }bookingdata;
    
       bookingdata record[100];
    
       FILE * filehandle;
       char line[MAXLEN];
    
       char *item = NULL;
       int reccount = 0;
       int k;
    
       /* Here comes the actions! */
       /* open file */
    
       filehandle = fopen("booking.txt","r");
    
       /* Read file line by line */
    
       while (fgets(line,MAXLEN,filehandle)) 
          {
              printf("%s",line);
    
              item = strtok(line," ");
              strcpy(record[reccount].reservationId,item);
    
              item = strtok(NULL," ");
              strcpy(record[reccount].customerId,   item);
    
              item = strtok(NULL," ");
              strcpy(record[reccount].roomId,   item);
    
              item = strtok(line," ");
              record[reccount].numGuests = atoi(item);
    
              item = strtok(NULL," ");
              strcpy(record[reccount].startDate,   item);
    
              item = strtok(NULL,"\n");
              strcpy(record[reccount].endDate,   item);
    
    
              //printf("%s\n",record[reccount].day);
              reccount++;
    
    
    
           }
    
       /* Close file */
    
       fclose(filehandle);
    
    
    /* Loop through and report on data */
    
    printf("Weather Record\n");
    for (k=0; k<reccount; k++) {
               printf("It is %s\n",record[k].reservationId);
    
    }
    }
    
    main()
    {
       checkin();
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    For starts move the struct typedef out of your checkin function...
    Place it at global scope right below your #includes...

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just use sscanf instead of strtok? You don't say where you get a segfault, or where you see any output. You should check to see that your file actually opens before you start trying to read from it. Do you see any lines output from your loop? How many? How many are in your file?


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Quote Originally Posted by quzah View Post
    You don't say where you get a segfault, or where you see any output. You should check to see that your file actually opens before you start trying to read from it. Do you see any lines output from your loop? How many? How many are in your file?


    Quzah.
    The segmentation error occurs here:

    Code:
    item = strtok(line," ");
              strcpy(record[reccount].reservationId,item);
    and the only line that outputs in the loop is the first printf.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Quote Originally Posted by CommonTater View Post
    For starts move the struct typedef out of your checkin function...
    Place it at global scope right below your #includes...
    Ha! Sorry about that. Although I'm still getting the segmentation error.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    How long is your reservationID? If it's 7 characters you don't have enough room in your struct for it...

    A string of 7 characters requires 8 bytes of space... Every string requires at least 1 extra byte to hold the trailing null.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while( fgets( line, MAXLEN, filehandle ) != NULL )
    {
        printf( "%s\n", line );
        if( sscanf( line, "%6s %6s %4s %d %9s %9s %9s",
            record[reccount].reservationId, record[reccount].customerId,
            record[reccount].roomId, &record[reccount].numGuests,
            record[reccount].startDate, record[reccount].endDate,
            record[reccount].status ) != 7 )
        {
            ...error...
        }
        else
        {
            ...yay...
        }
    }
    Try that.


    Quzah.
    Last edited by quzah; 04-26-2011 at 02:37 PM.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    @CommonTater

    THe length of the reservationId is 6 characters hence why I put 7.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Read up on strtok. If there's no space character in line, item will be null, which is probably the cause of your seg fault. Just because the first printf in the loop is the last output you see doesn't mean that the next line causes the seg fault. What is the exact contents of line that are printed just before the crash? If you have only 2 spaces in there, the third or fourth strtok will fail. Can you run this in a debugger and step through to see where it actually seg faults? If not, print out each item immediately after you read it it.

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Quote Originally Posted by anduril462 View Post
    Read up on strtok. If there's no space character in line, item will be null, which is probably the cause of your seg fault. Just because the first printf in the loop is the last output you see doesn't mean that the next line causes the seg fault. What is the exact contents of line that are printed just before the crash? If you have only 2 spaces in there, the third or fourth strtok will fail. Can you run this in a debugger and step through to see where it actually seg faults? If not, print out each item immediately after you read it it.
    This is the line that gets printed before the crash anduril:

    RES001;CUS001;R101;1;05/06/2011;08/06/2011;reserved
    Segmentation fault

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by david.jones View Post
    This is the line that gets printed before the crash anduril:

    RES001;CUS001;R101;1;05/06/2011;08/06/2011;reserved
    Segmentation fault
    Why are you using " " when you should be using ";" for strtok?


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Quote Originally Posted by quzah View Post
    Why are you using " " when you should be using ";" for strtok?


    Quzah.
    sorry about that. I was playing around with code while waiting for a response so I copied the bit I modified.
    However i still receive a segmentation error.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    As anduril462 says, each strtok() result needs to be checked for NULL before you use it.
    Code:
    item = strtok(line," ");
    if ( item != NULL ) {
        strcpy(record[reccount].reservationId,item);
    } else {
        // do something else, say error message
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by david.jones View Post
    @CommonTater

    THe length of the reservationId is 6 characters hence why I put 7.
    Excellent. You'd be surprised now many people don't think about that.

  15. #15
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    nevermind
    Last edited by david.jones; 04-26-2011 at 03:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation error
    By callkalpa in forum C Programming
    Replies: 2
    Last Post: 12-13-2009, 03:27 AM
  2. segmentation error
    By akono in forum C Programming
    Replies: 3
    Last Post: 04-26-2009, 07:29 AM
  3. Segmentation Error / Bus Error in ANSI
    By drag0n69 in forum C Programming
    Replies: 10
    Last Post: 02-05-2008, 09:45 AM
  4. Segmentation error
    By Kitty Litter in forum C Programming
    Replies: 5
    Last Post: 11-07-2006, 09:35 PM
  5. What is a segmentation error?
    By Crankit211 in forum C Programming
    Replies: 3
    Last Post: 12-11-2001, 01:08 AM