如何解决在C ++中通过管道从子进程读取和写入
我正在尝试从当前正在运行且非常挣扎的可执行文件中读取输出。这是我用c / c ++编写的uni的蛮力项目。我对c / c ++还是很陌生,事实证明比我想象的要困难得多。我运行一个可执行文件,要求输入密码(len 3),然后将我的猜测(存储在Guesses数组中)写入正在运行的程序(没关系),但是在运行时无法读取。
//pipe initial variables
char Buffer[BUFSIZ];
memset(Buffer,'\0',sizeof(Buffer));
for (int i=0; i<1000; i++){
// open pipe
FILE *WritePipe = popen(("./"+Target[0]).c_str(),"w"); // write pipe to executable
FILE *ReadPipe = popen(("./"+Target[0]).c_str(),"r"); // read from running executable?
char Data[1024];
// if pipe failed to open
if (WritePipe == NULL || ReadPipe == NULL){
return "Pipe faliure\n" + to_string(EXIT_FAILURE);
}
// write guess to file via pipe
fprintf(WritePipe,"%s",(Guesses[i]+'\n').c_str());
pclose(WritePipe); // close write pipe
// while not end of file
while (!feof(ReadPipe)){
fgets(Data,BUFSIZ,ReadPipe); // problem here!
} // only last line is read
// close read pipe
pclose(ReadPipe);
//cout << Data << endl;
if (Data == Target[2]){
return Guesses[i];
}
}
return "password not found";
大多数运行良好(对代码呕吐我感到很抱歉,我认为这对于上下文来说是一件好事),但事实证明,读取结果对我来说是不可能的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。