How I can create const char* []

  argv, c++, c++11, xcode

I have code:

int ParseCommandLine( int argc, const char* argv[])
{
    string  inFilePath = "";
    string outFilePath = "";

    for( int i = 1; i < argc; ++i )
    {
        if( string( argv[i] ) == "-i" || string( argv[i] ) == "--input")
        {
            // Check for "-i @args" form of reqest.
            if( argc <= 2 )
            {
                ifstream newIn;
                newIn.open(string(argv[++i]));
            
                if(!newIn.is_open())
                {
                    cerr << "Incorrect file path.";
                    return 1;
                }
            
                string buff;
                vector<string> file;
            
                while(newIn >> buff)
                    file.push_back(buff);
            
                char**  arr = new char* [file.size()];
            
                for(int j = 0; j < file.size(); ++j)
                    arr[j] = file[j].c_str();    // Error: Assigning to 'char *' from incompatible type 'const std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::value_type *' (aka 'const char *')
            
                ParseCommandLine(file.size(), arr);    // Error No matching function for call to 'ParseCommandLine'
            
                delete[] arr;
                newIn.close();
            
                return 0;
            }
            else
            {
                cerr << "Reqest with --input option can have only one argument." << endl;
                return 1;
        }
    }

The function ParseCommandLine getting argc&argv from the main params. This piece of program need to transform new file to const char* argv[] and call the ParseComandLine secondly.

Anybody can help with this problem?

Source: Windows Questions C++

LEAVE A COMMENT