Search code examples
c++11stdvisual-studio-2019wstring

How do i pass a variable as wchat_t** from a std::wstring in c++


I am using VS 2019 and the C++ Language Standard is set to Default which I assume is C++ 11?

I have the following constructor of a class in a header file:

input_parser(int& argc, wchar_t** argv)
{
    for (auto i = 0; i < argc; ++i)
    {
        this->tokens_.emplace_back(argv[i]);
    }
};

To call the methods argv parameter I am creating an array of wchar_t in the following manner:

std::wstring command_line = L"-m \"F-14RHV\" -s \"BIT|Flir\" -d";
auto buffer = new wchar_t[command_line.length() + 1];
wcsncpy_s(buffer, command_line.length()+1, command_line.c_str(), command_line.length() + 1);
const auto inputs = input_parser(argc, &buffer);
delete[] buffer;

Inside the constructor the first pass when argc == 0 is fine but I get an access violation when argc == 1.


Solution

  • Okay so some programmer dude was correct and here is how I have to do it after I figure out how to split the string by spaces!

    Here is the final answer:

    #include <string>
    #include <sstream>
    #include <algorithm>
    #include <iostream>
    #include <iterator>
    
    std::wstring text = L"-m \"F-14RHV\" -s \"BIT|Flir\" -d";
    std::wistringstream iss(text);
    std::vector<std::wstring> results((std::istream_iterator<std::wstring, wchar_t>(iss)),
    std::istream_iterator<std::wstring, wchar_t>());
    

    Using a vector is going to make this process MUCH easier. I will probably change the other side to use a vector now.

    Thanks for the help.