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??
6 2940
Not sure.
I found a simple example here. -
// 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::ReadLine(); // added this.
-
return 0;
-
}
-
-
// compiled with VC++ 2005
-
And it worked fine. I made some small changes see the comments.
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.
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.
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 )
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.
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?
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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>
|
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...
|
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)...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |