小编Jan*_*nek的帖子

启动后写入NSTasks标准输入

我目前正试图绕过NSTask,NSPipe,NSFileHandle业务.所以我想我写了一个小工具,它可以编译和运行C代码.我还希望能够将我的stdout和stdin重定向到文本视图.

这是我到目前为止所得到的.我使用这篇文章中的代码来重定向我的stdio:在Cocoa中将stdout重定向到NSTextView的最佳方法是什么?

NSPipe *inputPipe = [NSPipe pipe];
// redirect stdin to input pipe file handle
dup2([[inputPipe fileHandleForReading] fileDescriptor], STDIN_FILENO);
// curInputHandle is an instance variable of type NSFileHandle
curInputHandle = [inputPipe fileHandleForWriting];

NSPipe *outputPipe = [NSPipe pipe];
NSFileHandle *readHandle = [outputPipe fileHandleForReading];
[readHandle waitForDataInBackgroundAndNotify];
// redirect stdout to output pipe file handle
dup2([[outputPipe fileHandleForWriting] fileDescriptor], STDOUT_FILENO);

// Instead of writing to curInputHandle here I would like to do it later
// when my C program hits a scanf
[curInputHandle …
Run Code Online (Sandbox Code Playgroud)

redirect stdin objective-c nstask nsfilehandle

5
推荐指数
1
解决办法
2444
查看次数

标签 统计

nsfilehandle ×1

nstask ×1

objective-c ×1

redirect ×1

stdin ×1