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

标签 c++ objective-c c piping

这是一个我整个下午都在研究的问题,我想我已经把它归结为它的核心问题,当通过管道将数据传入/传出 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/

相关文章:

c - 除了使用 malloc 之外,如何访问堆内存?

c++ - C++中的QRegExp捕获部分字符串

iphone - 匹配字典数组 - 其中一个数组不完全等于另一个数组

c - 在 C11 中使用 _Generic 宏失败

编译独立的 ext4 用户空间程序

ios - Applescript:写入新文件时为 "File Already Open"

C++ - 扩展数据类型

c++ - 如何从数组写入输出文件?

c++ - 不可调用对象的示例赋值运算符?

iphone - CLLocationManager 问题