繁体   English   中英

如何使用C ++执行极长的命令?

[英]How to execute extreme long command with C++?

我试图在我的C ++程序中执行相当长的外部命令(带有长参数的卷曲)。

在Linux上可以正常运行,但是如果“ cmd”长于127个字符,则Windows(在Cygwin下)始终显示“系统无法执行指定的程序”。

Linux接受几乎相同大小的相同代码。 尝试更改缓冲区的大小,无变化。

使用此代码:

#include <string>
#include <iostream>
#include <stdio.h>

std::string exec(char* cmd) {
    FILE* pipe = _popen(cmd, "r");
    if (!pipe) 
        return "ERROR";

    char buffer[128];
    std::string result = "";
    while(!feof(pipe)) {
        if(fgets(buffer, 128, pipe) != NULL)
            result += buffer;
    }
    _pclose(pipe);
    return result;
}

这是Windows的特定限制,还是可以通过某种方式扩展cmd缓冲区?

添加了如何创建要发送的char *:

   string message="This is message to send.";
   std::string send="";
   std::string curl_header="curl -s -F \"token=aE71iieVt8G1RWdJb59qmAa3hbxxxx\" -F \"user=uPKYQyNgGURbH1g56nBnjn9jNsxxxxx\" -F \"message=";
   send.append(curl_header);
   send.append(message);
   send.append("\" https://api.pushover.net/1/messages.json");
   cout << "\n";
   cout << "FULL STRING WITH PARAMS TO EXECUTE:";
   std::cout << send << '\n';
   char *cstr = new char[send.length() + 1];
   strcpy(cstr, send.c_str());
   cout << "FULL CHAR ARRAY WITH PARAMS TO EXECUTE:";
   std::cout << cstr << '\n';//this returns correct string, even longer than 127 chars
   cout << exec(cstr);// this returns error, same with system() if cstr is longer than 127 chars
    delete [] cstr;

您是否尝试过使用系统?

#include <iostream>

using namespace std;

int main(int argc,char** args){

if(argc<2){
    system("run run run run run run run run run run runr urn runrunrunru unr unrunr urn run runr unr urn run runrun runr unr urnrun runr urnurnrunrunrunrunrunrurn urnurnunrunrunrunrunrunrn u unru ru runr ur urn ru run rururnunrunrunr");
}else{
    printf("argc: %d",argc);
}
system("pause");
}

这似乎对我来说很好。 该程序名为run,因此它使用巨大的参数运行自身,然后输出看到的参数数量。 这是您要做什么?

暂无
暂无

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

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