简体   繁体   English

我如何创建 const char* []

[英]How I can create const char* []

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. function ParseCommandLine 从主参数获取 argc&argv。 This piece of program need to transform new file to const char* argv[] and call the ParseComandLine secondly.这段程序需要将新文件转换为 const char* argv[] 然后调用 ParseComandLine。

Anybody can help with this problem?任何人都可以帮助解决这个问题?

Just change char to const char只需将char更改为const char

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM