
怎样用c++重定向一个可执行文件(linux下)
#include<iostream>usingnamespacestd;intmain(){inta,b;cin>>a;cin>>b;intc;c=a+b;cout<<c...
#include<iostream>
using namespace std;
int main()
{
int a,b;
cin>>a;
cin>>b;
int c;
c=a+b;
cout<<c;
return 0;
}
把这个程序编译成叫binary的可执行文件。
现在我想写另外的一个程序把这个程序的标准输入a,b都重定向到input.txt里面去。
标准输出也重定向到output.txt里面去。
我写的是
int main()
{
execl("./binary","binary","<input.txt",">output.txt"(char*)0);
return 0;
}
执行的时候跟执行binary是一样的,没有重定向;
现在我想达到./binary <input.txt >output.txt的效果
各路高人,求帮助啊!! 展开
using namespace std;
int main()
{
int a,b;
cin>>a;
cin>>b;
int c;
c=a+b;
cout<<c;
return 0;
}
把这个程序编译成叫binary的可执行文件。
现在我想写另外的一个程序把这个程序的标准输入a,b都重定向到input.txt里面去。
标准输出也重定向到output.txt里面去。
我写的是
int main()
{
execl("./binary","binary","<input.txt",">output.txt"(char*)0);
return 0;
}
执行的时候跟执行binary是一样的,没有重定向;
现在我想达到./binary <input.txt >output.txt的效果
各路高人,求帮助啊!! 展开
3个回答
展开全部
仅仅用execl是不可能的。可以尝试用system()函数。
execl的语法是*args是binary的输入参数。redirection “>" 和”<"不是binary的输入参数,而是linux shell的功能。
要用dup2与execl一起。这里提供一个网上别人的例子:www.unix.com/programming/53220-execl-redirecting-output-text-files.html
错误用法:
execl( "/bin/ls" , "-al" , '>' , "dirlist.txt" ,(char *) 0 );
和你的差不多。
别人建议正确代码(我没有试过):
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv)
{
int fd; /*file descriptor to the file we will redirect ls's output*/
if((fd = open("dirlist.txt", O_RDWR | O_CREAT))==-1){ /*open the file */
perror("open");
return 1;
}
dup2(fd,STDOUT_FILENO); /*copy the file descriptor fd into standard output*/
dup2(fd,STDERR_FILENO); /* same, for the standard error */
close(fd); /* close the file descriptor as we don't need it more */
/*execl ls */
execl( "/bin/ls" , "ls" , "-la" , (char *) 0 );
return 0;
}
execl的语法是*args是binary的输入参数。redirection “>" 和”<"不是binary的输入参数,而是linux shell的功能。
要用dup2与execl一起。这里提供一个网上别人的例子:www.unix.com/programming/53220-execl-redirecting-output-text-files.html
错误用法:
execl( "/bin/ls" , "-al" , '>' , "dirlist.txt" ,(char *) 0 );
和你的差不多。
别人建议正确代码(我没有试过):
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv)
{
int fd; /*file descriptor to the file we will redirect ls's output*/
if((fd = open("dirlist.txt", O_RDWR | O_CREAT))==-1){ /*open the file */
perror("open");
return 1;
}
dup2(fd,STDOUT_FILENO); /*copy the file descriptor fd into standard output*/
dup2(fd,STDERR_FILENO); /* same, for the standard error */
close(fd); /* close the file descriptor as we don't need it more */
/*execl ls */
execl( "/bin/ls" , "ls" , "-la" , (char *) 0 );
return 0;
}
展开全部
您需要设置的环境变量CL和连接器,如win7下的路径是C:\程序文件(x86)\微软的Visual Studio 8 \ VC \ BIN
您可以设置以下步骤a。问题百度HI我的步骤如下:
首先,打开接口配置环境变量,如下:我的电脑---属性---高级---环境变量
编辑PATH变量的最后如果没有分号,添加一个分号,那么你cl和连接器的目录的完整路径背后还记得添加一个分号,以确定PATH变量可以。请记住,重新打开cmd窗口。
另外,在虚拟机超低价团购,
您可以设置以下步骤a。问题百度HI我的步骤如下:
首先,打开接口配置环境变量,如下:我的电脑---属性---高级---环境变量
编辑PATH变量的最后如果没有分号,添加一个分号,那么你cl和连接器的目录的完整路径背后还记得添加一个分号,以确定PATH变量可以。请记住,重新打开cmd窗口。
另外,在虚拟机超低价团购,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我用sh可以实现
想来cstdlib的system也可以
想来cstdlib的system也可以
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询