gpt4 book ai didi

c - 如何使用 _spawnvp() 避免空间 split 和引用删除

转载 作者:可可西里 更新时间:2023-11-01 09:31:35 25 4
gpt4 key购买 nike

在 Unix 上,我们可以使用 fork() 创建一个新进程; execvp(argv[0], argv); (如果我们是跟随 fork 的 parent 或 child ,则需要一些管道)。在子进程中,main(argc, argv) 将完全按照传递给 execvp 的方式查看字符串。

在 Windows 上,_spawn() 系列基本上实现了 fork();执行();一步到位。到目前为止,太好了。问题是当我们到达子进程的 main() 时,我们的字符串已经不是原来的样子了。让我举个例子。

argv[0] = "foo";
argv[1] = "bar";
argv[2] = "Use spaces and \"quotes\"";
_spawnvp(0, argv[0], argv);

当我们到达子节点时,在 main() 中我们会发现,在此示例中,argv[0] 和 argv[1] 符合预期,但 argv[2] 已在空格上标记化并删除了引号,这样那个

argv[2] == "Use"
argv[3] == "and"
argv[4] == "quotes"

如何将 argv 结构按原样从父级传递给子级,并对其进行重新解释和更改?

最佳答案

根据 MSDN 上的注释的文档,如果您希望将包含空格的字符串作为单个参数传递,则需要将其引用。

Spaces embedded in strings may cause unexpected behavior; for example, passing _spawn the string "hi there" will result in the new process getting two arguments, "hi" and "there". If the intent was to have the new process open a file named "hi there", the process would fail. You can avoid this by quoting the string: "\"hi there\"".

关于c - 如何使用 _spawnvp() 避免空间 split 和引用删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4146980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com