本文整理汇总了C++中CmdLineArgs::begin方法的典型用法代码示例。如果您正苦于以下问题:C++ CmdLineArgs::begin方法的具体用法?C++ CmdLineArgs::begin怎么用?C++ CmdLineArgs::begin使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CmdLineArgs
的用法示例。
在下文中一共展示了CmdLineArgs::begin方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getCommandOutPut
std::string getCommandOutPut(const std::string& cmd, CmdLineArgs& cmdArgs,
unsigned char* stdinput, const unsigned stdinputlength)
{
int outfd[2];
int infd[2];
int oldstdin, oldstdout;
pipe(outfd); // Where the parent is going to write to
pipe(infd); // From where parent is going to read
oldstdin = dup(0); // Save current stdin
oldstdout = dup(1); // Save stdout
close(0);
close(1);
dup2(outfd[0], 0); // Make the read end of outfd pipe as stdin
dup2(infd[1], 1); // Make the write end of infd as stdout
if(!fork()) {
//std::cerr<< "No. of cmd args : " << cmdArgs.size() << std::endl;
char **argv = new char*[2 + cmdArgs.size()];
argv[0] = (char*) cmd.c_str();
int i = 1;
for(CmdLineArgs::iterator itr = cmdArgs.begin(); itr != cmdArgs.end(); ++itr) {
argv[i++] = (char*) itr->c_str();
std::cerr << "Arg : \n" << itr->c_str() << "\n";
}
argv[i] = 0;
close(outfd[0]); // Not required for the child
close(outfd[1]);
close(infd[0]);
close(infd[1]);
execv(argv[0],argv);
}
else {
close(0); close(1);
dup2(oldstdin, 0);
dup2(oldstdout, 1);
close(outfd[0]); // These are being used by the child
close(infd[1]);
if( stdinput && stdinputlength ) {
write(outfd[1],stdinput, stdinputlength ); // Write to childâs stdin
close(outfd[1]);
}
char input[1024] = { 0 };
int bytesRead = 0;
std::string retVal;
while( bytesRead = read(infd[0], input, 1024) ) {
retVal += std::string(input);
memset(input, 0, sizeof(input));
}
//char input[1024] = { 0 };
//input[read(infd[0], input, 1024)] = 0; // Read from childâs stdout
//std::string retVal(input);
return retVal;
}
}