简体   繁体   English

C / C ++指针和数组帮助

[英]C/C++ Pointers and Arrays help

I have the following C++ code and I can't seem to get it working. 我有以下C ++代码,但似乎无法正常工作。 What I am trying to do is read numerous entries from the command line, separated by ('|') pipe characters, and then splitting the resulting strings by spaces. 我想做的是从命令行读取大量条目,并用('|')竖线字符分隔,然后将结果字符串用空格分隔。

eg. 

mkdir C:/unixcode/shells|cd D:/margins/code | pwd| finger kobojunkie | last -l kobojunkie

but so far, I get errors, something about declaring the size of the pointer: 但是到目前为止,我得到了一些错误,这与声明指针的大小有关:

Initializer fails to determine the size of argv2 cannot convert char** to char* for argument 1 to char strtok(char*, const char*) 初始化程序无法确定argv2的大小,无法将参数1的char**转换为char*char strtok(char*, const char*)

int main(int argc, char *argv[])
{
    char * pch;
    pch = strtok (argv,"|");
    //parse the contents of the generated arrays
    while (pch != NULL)
    {
        printf ("%s\n",pch);
        char * argv2[] = pch;
        char * subpch = strtok(argv2," ");

        while (subpch !=NULL)
        {
            printf ("%s\n",subpch);
            subpch = strtok (NULL, " ");
        }
        pch = strtok (NULL, " ");
    }
    return 0;
}

the type of argv is char** , not char* hence you cannot pass it to strtok. argv的类型为char** ,而不是char*因此您无法将其传递给strtok。 Use argv[ 1 ] instead, but check that argc >= 2 first. 请改用argv [1],但请先检查argc> = 2。

Or, since this is tagged c++, use stl to split the string, eg 或者,由于这是标记为c ++的代码,因此请使用stl拆分字符串,例如

void split( const std::string& s, char delim, std::vector<std::string>& elems )
{
  std::stringstream ss( s );
  std::string item;
  while( std::getline( ss, item, delim ) )
    if( !item.empty() )
      elems.push_back( item );
}

int main( int argc, char *argv[] )
{
  if( argc == 2 )
  {
    std::vector< std::string > elements;
    split( argv[ 1 ], '|', elements );
    //elements now contains all items..
  }
}

argv is an array of arrays pointers. argv 数组 指针的 数组 You cannot pass it as is to strtok: you need to pass its elements in a loop 您无法按原样传递它:您需要循环传递其元素

for (k = 1; k < argc; k++) {
    pch = strtok(argv[k], "|");
    /* ... */
}

Also: are you sure you want to delimit with "|" 另外:确定要用"|"定界 ? That character has a special meaning to shells and, usually, does not make it to your program. 该字符对shell具有特殊的含义,通常不会在您的程序中使用。 Unless you call your program with them escaped, eg 除非您在调用程序时将它们转义,例如

bash$ ./a.out 'one|two|three' 'four|five|six'

The command-line is managed by a program: the shell (probably cmd.exe in Windows or bash in Linux). 命令行由程序管理:外壳程序(在Windows中可能是cmd.exe ,在Linux中可能是bash )。 That shell gets all the stuff written in the command line and parses it and executes the commands specified. 该shell获取在命令行中编写的所有内容,并对其进行解析并执行指定的命令。

Unless you are writing a shell, you cannot ever see the "|" 除非您正在编写外壳程序,否则您将永远看不到"|" of your example command-line inside the programs you write. 您编写的程序中的示例命令行示例。 They are effectively processed from the shell and removed from the parameters sent to the programs. 它们是从Shell中有效处理的,并从发送给程序的参数中删除。

In

mkdir C:/unixcode/shells|cd D:/margins/code | pwd| finger kobojunkie | last -l kobojunkie

the shell calls the 5 following commands, each with the parameters specified Shell调用以下5个命令,每个命令都指定了参数

  • mkdir C:/unixcode/shells mkdir C:/ unixcode /壳
  • cd D:/margins/code cd D:/边距/代码
  • pwd PWD
  • finger kobojunkie 手指kobojunkie
  • last -l kobojunkie 最后-l kobojunkie

Note none of the programs receive a "|" 请注意,所有程序均未收到"|" .


If you are indeed writing a shell, the command-line is not available in the argv array. 如果确实在编写Shell,则argv数组中不提供命令行。 It depends on the way you manage input inside your shell. 这取决于您在Shell中管理输入的方式。

argv is not a string. argv不是字符串。 argv is an array of strings. argv是字符串数组 strtok takes a string, so you cannot pass it an array of strings and expect it to do something meaningful. strtok需要一个字符串,因此您不能将它传递给字符串数组,并且不能期望它做有意义的事情。

Each string element of the argv array is a separate command line parameter, except for the first which is the name of the executable. argv数组的每个字符串元素都是一个单独的命令行参数,但第一个参数是可执行文件的名称。 So what you should be doing is looking through each string entry for "|", and acting accordingly. 因此,您应该做的是在每个字符串条目中查找“ |”,并采取相应的措施。

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

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