473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

MSVC can't find strtok()?

121 100+
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??
Mar 12 '08 #1
6 2940
MACKTEK
40
Not sure.
I found a simple example here.

Expand|Select|Wrap|Line Numbers
  1. // StrTokens_Forum.cpp : main project file.
  2. /* strtok example */
  3. #include "stdafx.h"  // added this.
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. using namespace System;  // added this
  8. int main ()
  9. {
  10.   char str[] ="- This, a sample string.";
  11.   char * pch;
  12.   printf ("Splitting string \"%s\" into tokens:\n",str);
  13.   pch = strtok (str," ,.-");
  14.   while (pch != NULL)
  15.   {
  16.     printf ("%s\n",pch);
  17.     pch = strtok (NULL, " ,.-");
  18.   }
  19.   Console::ReadLine();  // added this.
  20.   return 0;
  21. }
  22.  
  23. // compiled with VC++ 2005
  24.  
And it worked fine. I made some small changes see the comments.
Mar 12 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
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.
Mar 12 '08 #3
oler1s
671 Expert 512MB
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.
Mar 12 '08 #4
Banfa
9,065 Expert Mod 8TB
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 )
Mar 12 '08 #5
Sick0Fant
121 100+
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.
Mar 13 '08 #6
oler1s
671 Expert 512MB
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?
Mar 13 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
3
by: New | last post by:
I have a String from a file in the form <number> <word>,<word>,<word>,<word> <number> when I try to tokenize it I store it in a char and tokenize it using strtok(string, " ") and then tokenize the...
8
by: manochavishal | last post by:
Hi I am writing a Program in which i get input as #C1012,S,A#C1013,S,U I want to get C1012,S,A using strtok and then pass this to function CreateVideo which will further strtok this...
2
by: manochavishal | last post by:
Hi I am writing a Program in which i get input as #C1012,S,A#C1013,S,U I want to get C1012,S,A using strtok and then pass this to function CreateCopies which will further strtok this...
3
by: nomad5000 | last post by:
Hi everybody! I'm having trouble using strtok to fill a matrix with int nrs. from a file. the code that is not working is the following: #include <iostream> #include <fstream> #include...
5
by: Kelly B | last post by:
I need a function which returns me a "word" from a given string and then sets the pointer to the next one which is then retrieved during further calls to the function. I think strtok( ) is the...
29
by: Pietro Cerutti | last post by:
Hello, here I have a strange problem with a real simple strtok example. The program is as follows: ### BEGIN STRTOK ### #include <string.h> #include <stdio.h>
17
by: fl | last post by:
Hi, I am learning C++ from the following C++ website, which has some very good small examples. For the second Fraction example, which has five files: Main.cpp Fraction.cpp Fraction.h...
12
by: Pilcrow | last post by:
Here is a quick program, together with its output, that illustrates what I consider to be a deficiency of the standard function strtok from <string.h>: I am using C:\>gcc --version gcc (GCC)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.