Getting segmentation fault with strtok()

This is a discussion on Getting segmentation fault with strtok() within the C Programming forums, part of the General Programming Boards category; Right now I'm trying to parse a CSV file however I keep receiving a segmentation fault. Below is part of ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    27

    Getting segmentation fault with strtok()

    Right now I'm trying to parse a CSV file however I keep receiving a segmentation fault. Below is part of my code where it is happening.

    Code:
    count = 1;
    		
    while(1){
      if(count == 20) break;
      curr = strtok(NULL,",");
      printf("testing\n");
      if((*curr == '"') && (curr[strlen(curr) - 1] != '"')){
        strcpy(hold,curr);
        isq = 1;
        while(isq == 1){
          curr = strtok(NULL,",");
          strcat(hold,",");
          strcat(hold,curr);
          if(curr[strlen(curr) - 1] == '"') isq = 0;
        }
        strcpy(curr,hold);
      }
      count++;
      printf("%s\n",curr);
    }
    I can't simply use strtok() to get all the data from the CSV file because there could be a "sdf,lkf" type entry in the CSV file.

    Right now I receive a segmentation fault whenever the strtok() runs into a "sdfk,lskjfl" type entry.

    If anybody could see why this is happening or perhaps a better way to do this it would be appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I assume that before the first strtok call, that you have actually called it with a different string, right? Because you're not actually doing that anywhere.


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

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    What about if the return value of strtok() is NULL (which it uses to indicate that no matching tokens were found)?

    [edit] Oh, and as quzah said, the first time you call strtok() you're supposed to pass the string you want it to tokenize . . . . [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What's the definition of hold.

    You don't check curr for NULL, so if you ever end up back at the strtok with no comma, the line that checks for " will fail.

    And I personally prefer a solution that does something like this:
    Code:
    look at current character
    if quote, then copy until next quote
    else if comma, save current string, set string to empty
    else add char to current string.
    All that strtok/strcat/strcpy seems awfully complicated for such a simple task.

    --
    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.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Albinoswordfish View Post
    Code:
      printf("testing\n");
    It may (or may not) make a difference in your debugging here, but for future reference *always* follow a debugging printf() with fflush(stdout):
    Code:
    printf("testing\n"); fflush(stdout);
    This is because the stdout buffer is probably not flushed just by a '\n'; it may be held longer than than that. So, if you are moving that line around, or using several such printf() lines to isolate a seg fault, you *can* be misled as to where it occurs if you do not use fflush(stdout).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I was under the impression that stdout was automatically flushed by a '\n' when stdout refers to a terminal. I'm not sure how portable this is, though, and a fflush() never hurts (unless you do it on stdin...).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 09:32 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, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

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