I get this error code in VS: 0x80070002 c++

0

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.

c++
string
visual-studio
asked on Stack Overflow Nov 17, 2020 by w0lfey13 • edited Nov 18, 2020 by w0lfey13

1 Answer

0

There are two methods to meet your needs;

  1. Add _CRT_SECURE_NO_WARNINGS in Properties->C/C++->Preprocessor->Preprocessor Definitions.

  2. 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; 
     }
    
answered on Stack Overflow Nov 20, 2020 by Barrnet Chou • edited Nov 20, 2020 by Barrnet Chou

User contributions licensed under CC BY-SA 3.0