在 Xcode 4 中重定向 I/O [英] Redirecting I/O in Xcode 4

查看:25
本文介绍了在 Xcode 4 中重定向 I/O的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚安装了 Xcode 4,我正在尝试将输入从文件重定向到我的 C++ 程序.我试过在我的运行方案的参数"部分使用通常的< infile.txt",但这没有用.我能够很好地重定向 Xcode 3 中的输入和输出(通过编辑可执行文件的参数).有关如何在新版本中执行此操作的任何建议?

I just installed Xcode 4 and I'm trying to redirect input from a file to my C++ program. I've tried using the usual "< infile.txt" in the "Arguments" section of my Run scheme, but that didn't work. I was able to redirect input and output in Xcode 3 just fine (by editing the arguments for the executable). Any suggestions on how to do this in the new version?

谢谢!

萨默

推荐答案

我用各种类型的 Arguments 进行了测试,似乎 Xcode 有一个关于 Arguments 的错误(上次测试:Xcode 8).

I tested with various types of Arguments and it appears that Xcode have a bug with Arguments (last test: Xcode 8).

但是有一种替代方法可以模拟类似的效果.您必须使用环境变量.

But there is one alternative to simulate with similar effect. You must use the Environment Variables.

使用要重定向的文件名添加环境变量:

Add a Environment Variable with the file name you want to redirect:

然后在你的代码中你必须重定向"这个文件到标准输入(cin):

Then in your code you must "redirect" this file to the standard input (cin):

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main (int argc, const char * argv[])
{
    ifstream arq(getenv("MYARQ"));
    cin.rdbuf(arq.rdbuf());

    string value;
    cin >> value;
    cout << value;

    return 0;
}

就是这样...只有两行代码

that's it... only 2 lines of code

ifstream arq(getenv("MYARQ"));
cin.rdbuf(arq.rdbuf());

这不是最好的解决方案,但虽然 xcode 有这个问题,但这是唯一的解决方案!

it's not the best solution, but while xcode have this problem this is the only solution !

这篇关于在 Xcode 4 中重定向 I/O的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆