这是我整个下午一直在努力的一个问题,我想我已经把它减少到了它的核心问题,当从/从Objective A命令行应用程序传输数据时,这似乎是意外的行为C ++应用程序。
单独执行时,Objective C程序按预期工作。当C ++管道(在这种情况下是“主”,C ++调用Objective C可执行文件)调用类似于下面的Objective C代码的C / C ++可执行文件时,一切都按预期工作。
此外,如果输入代码从Objective C中删除,或者C ++程序将Objective C命令管道传输到文件(因此命令将是“./HelloWorld> dump.txt”而不是“。 / HelloWorld“)一切都按预期执行。
但是,当执行下面的代码时,在尝试读取Objective C的stdout时,C ++会在第一次尝试之前挂起,然后才会在Objective C中尝试读取stdin。
目标C
#import <Foundation/Foundation.h>
void c_print(NSString* prnt)
{
printf("%s", [prnt cStringUsingEncoding:NSUTF8StringEncoding]);
}
void c_print_ln(NSString* prnt)
{
printf("%s\n", [prnt cStringUsingEncoding:NSUTF8StringEncoding]);
}
NSString* read_till(char c)
{
NSMutableString* ret = [[NSMutableString alloc] initWithString:@""];
char r = getchar();
while(r!=c && r!= '\0')
{
[ret appendFormat:@"%c",r];
r = getchar();
}
return ret;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
c_print_ln(@"Hello, World!");
NSString* exmp = read_till('\n');
c_print_ln([[NSString alloc] initWithFormat:@"String I read: \"%@\"",exmp]);
}
return 0;
}
C ++(。h文件)
#ifndef PIPE_H
#define PIPE_H
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <string>
#include <iostream>
#define PIPE_READ 0
#define PIPE_WRITE 1
class outsideExecutable
{
private:
char buf[1024];
bool is_good;
int infp, outfp;
public:
outsideExecutable(char* command);
~outsideExecutable();
bool isGood();
std::string readline();
void writeline(std::string source);
};
#endif
C ++(。cpp文件)
#include "Pipe.h"
using namespace std;
int main()
{
cout<<"Testing Pipe"<<endl;
outsideExecutable* exe = new outsideExecutable((char*)"./HelloWorld");
exe->readline();
exe->writeline("reading example");
exe->readline();
delete exe;
}
static pid_t popen2(const char *command, int *infp, int *outfp)
{
int p_stdin[2], p_stdout[2];
pid_t pid;
if (pipe(p_stdin) != 0 || pipe(p_stdout) != 0)
return -1;
pid = fork();
if (pid < 0)
return pid;
else if (pid == 0)
{
close(p_stdin[PIPE_WRITE]);
dup2(p_stdin[PIPE_READ], PIPE_READ);
close(p_stdout[PIPE_READ]);
dup2(p_stdout[PIPE_WRITE], PIPE_WRITE);
execl("/bin/sh", "sh", "-c", command, NULL);
perror("execl");
exit(1);
}
if (infp == NULL)
close(p_stdin[PIPE_WRITE]);
else
*infp = p_stdin[PIPE_WRITE];
if (outfp == NULL)
close(p_stdout[PIPE_READ]);
else
*outfp = p_stdout[PIPE_READ];
return pid;
}
outsideExecutable::outsideExecutable(char* command)
{
is_good = false;
if (popen2(command, &infp, &outfp) <= 0)
return;
is_good = true;
}
outsideExecutable::~outsideExecutable()
{
}
bool outsideExecutable::isGood()
{
return is_good;
}
std::string outsideExecutable::readline()
{
if(!is_good)
return "";
string ret = "";
char hld;
read(outfp, &hld, 1);
while(hld!='\n' && hld!='\0')
{
ret = ret + hld;
read(outfp, &hld, 1);
}
cout<<"We read:"<<ret<<endl;
return ret;
}
void outsideExecutable::writeline(std::string source)
{
if(!is_good)
return;
//Do nothing
cout<<"Sending command: "<<source<<endl;
source = source+"\n";
write(infp, source.c_str(), source.length());
}
#endif
任何人都有任何想法可能有什么问题吗?我对C / C ++有很多经验,看来这方面的管道代码运行良好。看起来这似乎是Objective C的一个例子而不是很好玩,我从来没有见过像这样的管道失败的例子。
答案 0 :(得分:0)
rich(https://stackoverflow.com/users/1566221/rici)刚刚在上面的评论中提供了这个问题的答案。这是更新的Objective C代码来修复它:
void c_print(NSString* prnt)
{
printf("%s", [prnt cStringUsingEncoding:NSUTF8StringEncoding]);
fflush(stdout);
}
void c_print_ln(NSString* prnt)
{
printf("%s\n", [prnt cStringUsingEncoding:NSUTF8StringEncoding]);
fflush(stdout);
}
显然stdout需要在Objective C中刷新才能使管道正常工作。