- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在编写一个程序来执行带有任意数量参数的 Unix 命令。当我使用常规字符串时,我可以毫无问题地接收输入和制作 token 。但是,execvp 只接受一个指针数组,我不确定如何将字符串数组转换为此。这是我拥有的:
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <unistd.h>
int main()
{
while (true)
{
int argc = 0;
std::istringstream iss;
std::string command;
std::cout << "$> ";
getline(std::cin, command);
iss.str(command);
for (unsigned i = 0; i <= command.length(); i++)
{
if (command[i] == ' ' || command[i] == '\0')
{
argc++;
}
}
std::string arr[argc+1];
for (int i = 0; i < argc; i++)
{
iss >> arr[i];
}
if (arr[0].compare("quit"))
{
break;
}
else
{
char*argv[argc+1];
for (int i = 0; i < argc; i++)
{
argv[i] = arr[i].c_str(); //This line is wrong
}
argv[argc] = NULL;
execvp(argv[0], argv);
}
}
return 0;
}
我尝试了各种方法,但无法弄清楚如何以正确的方式将字符串转换为 char 数组。像 strcpy 这样的方法将不起作用,因为每个参数的长度会有所不同。任何帮助将不胜感激。
最佳答案
你有非常小的错误。
替换:
argv[i] = arr[i].c_str();
与 argv[i] = const_cast<char*>(arr[i].c_str());
if(arr[0].compare("quit"))
与 if(!arr[0].compare("quit"))
一切顺利,这适用于任何编译器。
运行 here
但我有一些建议,使用 fork 使其不会一次只运行一个命令。
示例 here
关于C++ 使用带字符串数组的 execvp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28712552/
POSIX 系统公开了一系列 exec 函数,这些函数允许将可能不同的东西加载到当前进程中,保持打开的文件描述符、进程标识符等。 这可以出于多种原因完成,在我的例子中,这是自举——我想更改我自己进程的
我有一个用 c 编写的程序,它使用 execvpe(3) 函数,并且我设置了一行以包含必需的头文件: #include 我使用以下命令编译此文件... gcc foo.c -o foo ...仅收到
我在 C 中有一个函数,它创建一个子进程并使其运行 execvp。 int Execute(char **arg) { pid_t pid; int status; if ((
我正在使用 C 编写一个非常基本的 UNIX shell。在这个项目中,我试图使用 fork() 和 execvp() 来执行实际的 shell命令。我遇到了一个问题,它似乎可以很好地处理具有单个参数
我无法获得使用 execvp 调用的程序的完整输出。在这里,我基本上是在尝试用一个可以过滤输入和输出的程序来包装任何程序: #define BUF_SIZ 8192 int main(int argc
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我正在尝试用 c 语言创建一个 shell,当前正在尝试 ls 命令,但子进程总是返回 execvp 无法工作。这是函数。首先,我使用 exec 获取我所在的路径和从 stdin 获取的命令,这就是为
我正在开发这个程序,使用系统调用 execvp() 和 fork() 来运行通过命令行参数给出的 shell 命令。这里 arglist 是一个二维数组,其中包含命令名称及其参数列表。我将命令名称作为
我尝试使用 execvp 创建一个作为参数给出的存储库。但我遇到了一些无法在以下代码中解决的段错误(核心转储): #include //mkdir #include #include //p
我正在尝试使用 execvp 对文件进行排序,这是我的代码。 char *argv1[]={ "sh", "-c", "sort input.txt > output.txt", NULL }; 问题
我正在c中测试execvpe(),我尝试了下面的代码,这导致错误“函数‘execvpe’的隐式声明在C99 [-Wimplicit-function-declaration]中无效”。 #includ
我有以下代码: int main(int argc, char *argv[]) { int i, a=9; int length = 0; c
我正在使用 execvp() 运行一些系统调用。程序对于有效命令非常有效,对于任何不存在的命令则失败,这是完美的。该程序是,当我在需要额外参数(如 cat)的命令上使用 execvp() 并且我不提供
用户将读取一行,我将保留第一个单词作为 execvp 的命令。 假设他将输入“cat file.txt” ...命令将为 cat 。但我不知道如何使用这个execvp(),我读了一些教程但仍然没有明白
我正在用 C 编程编写一个正在运行的 shell 脚本。我读过有关 exec 函数的内容,虽然不太了解,但我读过一个像这样使用 execvp 的示例 execvp(*argv, argv) ; /*
这是我的输入函数: int i,j,n,len,c; char *buffer = 0; size_t bufsize = 0; ssize_t characters;
我正在尝试编写一个程序,该程序将 fork ,然后打开一个文件并执行它。它应该执行的文件称为子文件,并且它已经被编译。当我输入 ./child 时,它就会运行。但是,当我运行该程序时,它不会执行子程序
所以我有一个 C 程序,它接受命令输入,但偶尔我似乎会遇到某种内存错误。 我如何读取输入: static void parseLine(char* commandLine, Sequence* seq
我尝试在终端中传递命令行参数“ls -l/user/myuser”并在主目录中执行 execvp。 但不知何故,当我调试代码时,它给了我这个错误。 int main(int argc, char *a
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我是一名优秀的程序员,十分优秀!