MSVC can't find strtok()?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sick0Fant
    New Member
    • Feb 2008
    • 121

    MSVC can't find strtok()?

    When I try to use strtok, I get an access violation. The program compiles and runs up to that point, but then MSVC asks me for the path of STRTOK.C and if I cancel that dialog, MSVC provides me with an endless list of assembly instructions.

    Obviously, I included the right header file (string.h) or it wouldn't have compiled. So-- any idea what's going on??
  • MACKTEK
    New Member
    • Mar 2008
    • 40

    #2
    Not sure.
    I found a simple example here.

    [code=cpp]
    // StrTokens_Forum .cpp : main project file.
    /* strtok example */
    #include "stdafx.h" // added this.

    #include <stdio.h>
    #include <string.h>
    using namespace System; // added this
    int main ()
    {
    char str[] ="- This, a sample string.";
    char * pch;
    printf ("Splitting string \"%s\" into tokens:\n",str) ;
    pch = strtok (str," ,.-");
    while (pch != NULL)
    {
    printf ("%s\n",pch) ;
    pch = strtok (NULL, " ,.-");
    }
    Console::ReadLi ne(); // added this.
    return 0;
    }

    // compiled with VC++ 2005
    [/code]

    And it worked fine. I made some small changes see the comments.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      An access violation is attempting to access an address that is outside your process address space or is a null address or you are trying to change something that is write-protected.

      You say the code compiled?

      STRTOK.C is the source file for strtok and you never compile it. Instead you use the C libraries. I don't understand your error. Functions are located by name at compile time or the code won't link.

      Comment

      • oler1s
        Recognized Expert Contributor
        • Aug 2007
        • 671

        #4
        MACKTEK, you should

        1. Learn to use code tags. It's not acceptable to post blocks of code without formatting them, as forum rules state.

        2. It's ok to not know the answer. It's not ok to pass off an answer when you don't know what you're talking about. For one thing, a code snippet doesn't answer the OP's question. For another, you don't even understand the code posted. "stdafx.h" is purely a Visual Studio thing. That snippet of code will not compile for me on gcc. And it's not pure C. It's C++/CLI.

        You make mistakes, fine. But don't derail the OP with your lack of knowledge, when there are others who can give proper answers.

        EDIT: My post came off unusually harsh. It's not like I don't appreciate the effort you put in. It's just that you can cause confusion in an already confused OP. I just want you to be more careful in posting answers. If you don't quite understand the code you're working with, you should wait to see if other people answer, before taking a guess. In this case, for example, you can confuse the OP with non C code, further prolonging his troubles.

        Sycophant:

        We like precise facts and observations. Show us a small snippet of code that replicates the problem you have. Make sure it compiles with no warnings. Then run and type verbatim the error you get.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Sick0Fant

          What is happening is that something about the way you have called strtok is causing an access violation, an exception. The debugger catches this and to help you it tries to display the code where it happened.

          However in this case it happened in the middle of the strtok function, the code for that function is not in your project (because it is part of the C standard library) so the debugger can not locate the code. If displays a box asking you to locate the code, you say you can't (press Cancel). At this point the debugger as to do something, it doesn't have the code so instead it displays the assembler at the location that caused the access violation.

          The thing to do at this point is you the call tree (a window in MSVC 6 can't remember in MSVC 2005 and I never used MSVC 2003) to step back up the call tree to the point where you called the standard library function, strtok, and you examine the value of the input parameters to the function to see if you can see what you have done wrong.

          If that doesn't work it wouldn't hurt to post a code snippet here so we can see and possibly advise you (remembering to use code tags of course :D )

          Comment

          • Sick0Fant
            New Member
            • Feb 2008
            • 121

            #6
            I found out what it was. I had been stepping through the code trying to debbug it. I had a statement like

            strtok(fn(input ), delims); //fn returns a char *

            The problem was in "fn." When I tried to step into the code, MSVC tried to step into strtok instead of fn.

            I was unfamiliar with this particular behavior of the debugger, since it always seemed to step over system library functions, etc.

            Turned out to be a relatively simple fix.

            Comment

            • oler1s
              Recognized Expert Contributor
              • Aug 2007
              • 671

              #7
              Aaahh, you were debugging the code. Letting us know about that in your original question would have helped! Could you post your solution or the link to it here, so other people can see it?

              Comment

              Working...