我正在尝试捕获使用Perl system函数执行的输出并将系统命令的ouptut重定向到文件,但由于某种原因我没有得到整个输出.
我正在使用以下方法:
system("example.exe >output.txt");
Run Code Online (Sandbox Code Playgroud)
这段代码有什么问题,还是有另一种方法可以做同样的事情?
我正在运行perl脚本中的命令行应用程序(使用system()),有时候不返回,确切地说它抛出异常,需要用户输入才能中止应用程序.此脚本用于使用system()命令自动测试我正在运行的应用程序.因为它是自动化测试的一部分,所以如果发生异常,sytem()命令必须返回并认为测试失败.
我想编写一段运行此应用程序的代码,如果发生异常,则必须继续使用脚本,因为此测试失败了.
一种方法是运行应用程序一段时间,如果系统调用没有在那段时间内返回,我们应该终止system()并继续脚本.(如何在Perl中终止带有警报的系统命令?)
实现此目的的代码:
my @output;
eval {
local $SIG{ALRM} = sub { die "Timeout\n" };
alarm 60;
return = system("testapp.exe");
alarm 0;
};
if ($@) {
print "Test Failed";
} else {
#compare the returned value with expected
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码在windows上不起作用我对此做了一些研究,发现SIG不能用于windows(书籍编程Perl).有人可能会建议我如何在Windows中实现这一目标?
我试图将char*转换为double并再次转换为char*.如果您创建的应用程序是32位但不适用于64位应用程序,则以下代码可以正常工作.当您尝试从int转换回char*时会发生此问题.例如,如果hello = 0x000000013fcf7888然后转换为0x000000003fcf7888,则只有最后32位是正确的.
#include <iostream>
#include <stdlib.h>
#include <tchar.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]){
char* hello = "hello";
unsigned int hello_to_int = (unsigned int)hello;
double hello_to_double = (double)hello_to_int;
cout<<hello<<endl;
cout<<hello_to_int<<"\n"<<hello_to_double<<endl;
unsigned int converted_int = (unsigned int)hello_to_double;
char* converted = reinterpret_cast<char*>(converted_int);
cout<<converted_int<<"\n"<<converted<<endl;
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 如何在perl脚本中生成其他程序并立即继续Perl处理(而不是停止直到生成的程序终止)?是否可以在生成的程序运行时处理来自生成程序的输出而不是等待它结束?