When i try to run a code with the function "strtok" in it i get the error code 0x80070002. I included cstring, cctype, string.h and i also tried using /DEBUG FULL in Properties - Linker - Debugging, like a few other posts said but it still doesn't work. Any clues why VS doesn't work with strtok? I also tried reinstalling VS and running a simple code like this:
#include <iostream>
#include <cstring>
#include <cctype>
#include <string.h>
using namespace std;
int main()
{
char s[100], * p;
cin.getline(s, 100);
p = strtok(s, " ");
cout << p;
return 0;
}
The desired behaviour would be to show me the first word of s. Even when i try to run the code at https://en.cppreference.com/w/cpp/string/byte/strtok i get the same error.
There are two methods to meet your needs;
Add _CRT_SECURE_NO_WARNINGS
in Properties->C/C++->Preprocessor->Preprocessor Definitions
.
Use strtok_s instead of strtok
:
int main()
{
char *buf;
char s[100], *p;
cin.getline(s, 100);
p = strtok_s(s, " ", &buf);
cout << p;
return 0;
}
User contributions licensed under CC BY-SA 3.0