Thread: why segmentation fault?

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    7

    why segmentation fault?

    Code:
     while( (ch = getchar())!=EOF)
     {
         do
         {
             hold[i++]=ch;
             ch=getchar();
         }while(ch != '\n'&& ch!=EOF);
    
         hold[i]='\0';
         i=0;
         teamname=strtok(hold, ",");
         lastname=strtok(NULL, ",");
         firstname=strtok(NULL, ",");
         email=strtok(NULL, ",");
    
         fprintf(output,"%s\n", teamname);
         fprintf(output,"%s\n", lastname);
         fprintf(output,"%s\n", firstname);
         fprintf(output,"%s\n", email);
    
         if(ch==EOF)
             return;
    }
    suppose to obtain 4 strings from comma separated lists...
    each list on a newline until the end of input file...

    and output into another file...
    gives segmentation fault...

    however, it works fine if i test the 4 string and fprintf only if all four variables are not null...

    don't understand how they could be null...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Exactly how are you reading these here? You state that they're being read from a file, but 'getchar' doesn't read from a file, it reads from stdin. Sure, technicly it's a file stream, but it's usually the keyboard, unless you've redirected it.

    Anyway, it sounds like you've already got it worked out. Since you don't show us the declarations for any of your variables, I'll assume you're using them all correctly.

    Show sample input, and sample output. You can debug this by adding a few simple if checks.
    Code:
    if( teamname )
        printf("%s\n", teamname );
    if( lastname )
        ...
    Then you can figure out why it's failing. But without seeing more of the code, and the provided input, I can only guess.

    Assuming your input is:
    Code:
    foo,bar,foobar,barfoo
    bar,foo,barfoo,foobar
    Your strtok would be wrong on the first occurance of 'barfoo'. Notice how you're calling the final strtok. You tell it you need a ',' for delimit. If that is the case, you sample input needs to be:
    Code:
    foo,bar,foobar,barfoo,
    bar,foo,barfoo,foobar,
    OR your final strtok call needs to be:
    Code:
    email=strtok( NULL, '\0' );
    And finally, you should always check your return values. You should likely be checking them as soon as you make each strtok call. If the first call fails, you don't want to be calling it three more times.

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

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    Code:
     int i=0,j=0;
     char hold[200], ch;
     char *teamname, *lastname, *firstname, *email;
     FILE *output = fopen("output", "w");
    
     while( (ch = getchar())!=EOF)
     {
         do
         {
             hold[i++]=ch;
             ch=getchar();
         }while(ch != '\n'&& ch!=EOF);
    
         hold[i]='\0';
         i=0;
         teamname=strtok(hold, ",");
         lastname=strtok(NULL, ",");
         firstname=strtok(NULL, ",");
         email=strtok(NULL, "\0");
    
         fprintf(output,"%s\n", teamname);
         fprintf(output,"%s\n", lastname);
         fprintf(output,"%s\n", firstname);
         fprintf(output,"%s\n", email);
    
         if(ch==EOF)
             return;
    yes, you are right about the last strtok...
    the input is by < inputfile...
    if i only print if its not null...
    ie.
    if(teamname) printf("%s",teamname)
    its fine but mysteriously prints 3 newline...
    but why would any of the string be NULL?
    its also fine if i input via the keyboard...
    thanks!
    Last edited by chris charlotte; 11-16-2003 at 04:34 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    My guess would be that your input file has extra blank lines at the end of the file, which would result in your final round of strtok() calls producing some unexpected results.

    Also, you should use fgets()
    Code:
     while( (ch = getchar())!=EOF)
     {
         do
         {
             hold[i++]=ch;
             ch=getchar();
         }while(ch != '\n'&& ch!=EOF);
    
         hold[i]='\0';
         i=0;
         /* rest of code */
    }
    Can be replaced with
    Code:
    while ( fgets( hold, sizeof hold, stdin ) != NULL ) {
         /* rest of code */
    }
    which has the added feature of preventing buffer overflow.
    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.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    that is what i thought... but when i run a program to count the number of newline, revealed that there is 4 newline for a file with 5 lines. ie. EOF is at the end of the last line...

    i use getchar because the input is to be entered via the keyboard...

    don't know why program < inputfile doesn't work the same way as when input is entered via keyboard.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    you are right, its the hidden newlines in the input file...
    also gets() will be better...

    thanks guys...

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Never use gets()
    Always use fgets()
    See the FAQ
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM