我目前正试图绕过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)