gpt4 book ai didi

c++ - 从 C/C++ 调用时,Objective C stdin/stdout 管道如何工作?

转载 作者:太空宇宙 更新时间:2023-11-04 04:37:53 25 4
gpt4 key购买 nike

这是一个我整个下午都在研究的问题,我想我已经把它归结为它的核心问题,当通过管道将数据传入/传出 Objective C 命令行应用程序(从C++ 应用程序。

单独执行时,Objective C 程序按预期工作。当 C++ 管道(在这种情况下是“主”,C++ 正在调用 Objective C 可执行文件)正在调用类似于下面的 Objective C 代码的 C/C++ 可执行文件时,一切也都按预期工作。

此外,如果输入代码从 Objective C 中删除,或者如果 C++ 程序命令将 Objective C 传送到文件(因此命令将是“./HelloWorld > dump.txt”而不是“./HelloWorld") 一切都按预期执行。

但是,当执行下面所示的代码时,C++ 在尝试读取 Objective C 的标准输出时挂起,这是在 Objective C 尝试读取标准输入之前的第一次尝试。

objective-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 表现不佳的一个例子,我从未见过像这样的管道失败的例子。

最佳答案

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);
}

显然需要在 Objective C 中刷新 stdout 才能使管道正常工作。

关于c++ - 从 C/C++ 调用时,Objective C stdin/stdout 管道如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29271383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com