简体   繁体   English

如何在 Mac 上使用 Xcode 在 C 中设置 I/O 重定向?

[英]How to set up I/O redirection in C with Xcode on a mac?

I'm writing a program and I need to set the program up to get input from a text file and save the output in a text file.我正在编写一个程序,我需要设置程序以从文本文件中获取输入并将输出保存在文本文件中。 I'm using Xcode and I'm on a mac.我正在使用 Xcode,我在 Mac 上。 I know I need to enter some redirection commands in the console but I'm not exactly sure how to do it.我知道我需要在控制台中输入一些重定向命令,但我不确定该怎么做。 For example text files feel free to use input.txt and output.txt.例如文本文件可以随意使用 input.txt 和 output.txt。

Thank you for the help.感谢您的帮助。

On the terminal (command line) you can do在终端(命令行)上,您可以执行

command < input.txt > output.txt

or或者

cat input.txt | command > output.txt

Edit:编辑:

Ok, thanks a lot. Just curious, when I type that in the terminal, 
how does it know to go to Xcode?

The only connection between running the command on the terminal and Xcode is that you build the program in Xcode.在终端上运行命令和 Xcode 之间的唯一联系是您在 Xcode 中构建程序。 That produces an executable that can be run either by Xcode or from the shell (terminal).这会生成一个可以由 Xcode 或 shell(终端)运行的可执行文件。 Running the program on the terminal has absolutely no effect on Xcode.在终端上运行程序对 Xcode 绝对没有影响。

There might well be a way to redirect I/O from within Xcode (perhaps that is what you originally wanted) but I am unaware of it (don't use Xcode).很可能有一种方法可以从 Xcode 内重定向 I/O(也许这就是您最初想要的),但我不知道(不要使用 Xcode)。

It seems difficult if not impossible to tell Xcode you wish to redirect standard input, output or error for your compiled command-line program.告诉 Xcode 您希望为编译的命令行程序重定向标准输入、输出或错误,这似乎很困难,如果不是不可能的话。 The Program > Scheme > Edit Scheme menu only helps out with application "Arguments Passes On Launch" [Cmd+<] which is something different than the redirection operators < and > in a shell. Program > Scheme > Edit Scheme 菜单只帮助应用程序“Arguments Passes On Launch” [Cmd+<] 这与 shell 中的重定向操作符 < 和 > 有所不同。 There seems no connection with a Terminal and Xcode, which also explains that your cannot debug a program launched from such external shell.终端和 Xcode 似乎没有联系,这也说明您无法调试从此类外部 shell 启动的程序。

As a basic workaround without dependencies, several sites suggest you would bake this redirection into your program's code.作为没有依赖项的基本解决方法,一些站点建议您将此重定向添加到您的程序代码中。 This can even be done conditionally for Apple macOS like:这甚至可以为 Apple macOS 有条件地完成,例如:

#ifdef __APPLE__
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
#endif

A slightly more elaborate example would leverage the "Environment Variables" as can be set on the very same Product Scheme tab and having program code like:一个稍微复杂一点的例子将利用“环境变量”,因为可以在相同的产品方案选项卡上设置,并具有如下程序代码:

#include <stdlib.h>

#ifdef __APPLE__
        if (getenv("STDIN")) {
            if (!freopen(getenv("STDIN"), "r", stdin)) {
                NSLog(@"Unable to redirect stdin to '%s'\n", getenv("STDIN"));
            }
        } else {
            NSLog(@"Unknown environment variable STDIN so assuming stdin\n");
        }
        if (getenv("STDOUT")) {
            if (!freopen(getenv("STDOUT"), "w", stdout)) {
                NSLog(@"Unable to redirect stdout to '%s'\n", getenv("STDOUT"));
            }
        } else {
            NSLog(@"Unknown environment variable STDOUT so assuming stdout\n");
        }
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM