execvp 不适用于 cut
execvp won't work with cut
我正在上学,当我尝试在c++中启动这个命令时,"cut"命令出现了问题。所以这是我的练习->我想在C++中启动这个命令->"cut-d':'-f5<file"我在主函数中把文本从文件写到变量输入。预期作为命令的结果->"five"但我只收到一条错误消息"cut:分隔符必须是单个字符有关详细信息,请尝试"cut-help"。"
你知道为什么吗?感谢帮助:)
这是我的测试代码:
#include <iostream>
#include <sys/wait.h>
#include <unistd.h>
#include <string>
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdlib>
const int BUFFER_SIZE = 4096;
using namespace std;
char *argCat[] = { "cat", (char *)0 };
char *argEcho[] = { "echo", "Hello", (char *)0 };
char *argCut[] = { "cut", "-d':'", "-f5", (char *)0};
char *stringCharConvertor(string paString)
{
char * temp = new char[paString.size() + 1];
std::copy(paString.begin(), paString.end(), temp);
temp[paString.size()] = ' ';
return temp;
}
void executeCommand(char** paCommand, string &paOutput, string &paInput)
{
char** arg = paCommand;
bool validInput = paInput == "" ? false : true;
int PARRENT_TO_CHILD[2];
int CHILD_TO_PARRENT[2];
if(pipe(CHILD_TO_PARRENT) < 0)
perror("pipe error");
if(validInput)
{
if(pipe(PARRENT_TO_CHILD) < 0)
perror("pipe error");
char* temp = stringCharConvertor(paInput);
write(PARRENT_TO_CHILD[1], temp, strlen(temp));
close(PARRENT_TO_CHILD[1]);
}
pid_t PID = fork();
if(PID > 0)
{
int s;
char buffer[BUFFER_SIZE+1];
memset(buffer, ' ', sizeof(buffer));
close[CHILD_TO_PARRENT[1]];
wait(&s);
if(read(CHILD_TO_PARRENT[0], buffer, BUFFER_SIZE) < 0)
printf("error ");
close(CHILD_TO_PARRENT[0]);
paOutput = buffer;
if(validInput)
close(PARRENT_TO_CHILD[0]);
cout << "n"+paInput;
}
else
if(PID == 0)
{
dup2(CHILD_TO_PARRENT[1], STDOUT_FILENO);
close(CHILD_TO_PARRENT[1]);
close(CHILD_TO_PARRENT[0]);
if(validInput)
{
dup2(PARRENT_TO_CHILD[0], STDIN_FILENO);
close(PARRENT_TO_CHILD[0]);
close(PARRENT_TO_CHILD[1]);
}
if(execvp(arg[0], arg) < 0)
close(CHILD_TO_PARRENT[1]);
}
}
int main()
{
string input = "one:two:three:four:five:six:seven:eight:nine:ten";
string output = "";
executeCommand(argCut, output, input);
cout << "n INPUT: "+input <<endl;
cout << "n OUTPUT: "+output <<endl;
return 0;
}
您应该尝试替换
char *argCut[] = { "cut", "-d':'", "-f5", (char *)0};
通过
char *argCut[] = { "cut", "-d:", "-f5", (char *)0};
无需报价
以下是一些基本原理:在代码上运行strace显示:
[pid 7641] execve("/usr/bin/cut", ["cut", "-d':'", "-f5"], [/* 85 vars */]) = 0
其或多或少等同于呼叫
/bin/cut "-d':'" "-f5"
其给出相同的错误
事实上,外壳确实删除了多余的引号,正如你所看到的:
$ echo one:two:three:four:five | strace -f /bin/cut -d':' -f5
execve("/bin/cut", ["/bin/cut", "-d:", "-f5"], [/* 85 vars */]) = 0
=> success
而:
$ echo one:two:three:four:five | strace -f /bin/cut "-d':'" -f5
execve("/bin/cut", ["/bin/cut", "-d':'", "-f5"], [/* 85 vars */]) = 0
=> failure
相关文章:
- FLTK 2.0构建和演示,适用于VS2019的2011年左右的代码库
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- 适用于 WebView2 旧版本的示例应用程序
- 在 NVIDIA GEFORCE GTX 1050 上下载适用于 Windows 10 的 openCL 1.2
- __attribute__(优化(0))) 是否适用于"recursively"?
- 为什么 std::erase(std::erase_if) 不是适用于<algorithm>任何容器的模板?
- 使用一个参数的模板函数时出错(适用于 2)
- 使用 适用于 Android 和 iOS 的 tf-lite C++ API
- 为什么这适用于 G++ 而不是 CLANG?
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- 适用于 Linux 的 c++ 上的代理脚本
- 为什么我的 SFINAE 表达式不再适用于 GCC 8.2?
- 使输出流式处理运算符适用于 boost::variant<std::vector<int>、int、double 的正确方法是什么>
- 有没有适用于Windows.lib文件的GNU二进制文件描述符(BFD)
- 模板函数仅适用于VS
- 如何在cmake中包含适用于g++或viceversa的库
- 适用于win32、linux、mac的POSIX C包装器
- WinDBG适用于从Visual Studio 2015保存的转储,但不适用于任务管理器。显示异常代码"not found"
- 从uint8_t到NPY_UINT16 PyArray_SimpleNewFromData.适用于Linux,但不适用于