在ANSI C ++中,如何将cout流分配给变量名?我想要做的是,如果用户指定了输出文件名,我在那里发送输出,否则,将其发送到屏幕。如下所示:

ofstream outFile;
if (outFileRequested) 
    outFile.open("foo.txt", ios::out);
else
    outFile = cout;  // Will not compile because outFile does not have an 
                     // assignment operator

outFile << "whatever" << endl;

我也尝试将其作为宏功能:

#define OUTPUT outFileRequested?outFile:cout

OUTPUT << "whatever" << endl;

但这也给了我编译错误。

我想我可以为每个输出使用IF-THEN块,但是如果可以的话我想避免使用它。有任何想法吗?

有帮助吗?

解决方案

使用参考。请注意,引用必须是 std :: ostream 类型,而不是 std :: ofstream ,因为 std :: cout std :: ostream ,所以你必须使用最小公分母。

std::ofstream realOutFile;

if(outFileRequested)
    realOutFile.open("foo.txt", std::ios::out);

std::ostream & outFile = (outFileRequested ? realOutFile : std::cout);

其他提示

我认为你的程序就像标准的unix工具一样,当没有给出文件时会写入标准输出,当给定文件时会写入该文件。您可以重定向 cout 以写入另一个流缓冲区。只要您的重定向处于活动状态,写入cout的所有内容都会透明地写入您指定的目标位置。一旦重定向对象超出范围,就会放入原始流并输出再次写入屏幕:

struct CoutRedirect { 
    std::streambuf * old; 
    CoutRedirect():old(0) {
        // empty
    }

    ~CoutRedirect() {
        if(old != 0) {
            std::cout.rdbuf(old);
        }
    }

    void redirect(std::streambuf * to) {
        old = std::cout.rdbuf(to);
    }
}

int main() {
    std::filebuf file;
    CoutRedirect pipe;
    if(outFileRequested) {
        file.open("foo.txt", std::ios_base::out);
        pipe.redirect(&file);
    }
}

现在,只要管道在main中存在,cout就会被重定向到该文件。你可以让它更“生产就绪”。使其不可复制,因为它还没有准备好被复制:如果副本超出范围,它将恢复原始流。

您可以在此处找到有关如何执行此操作的非常详细的说明: http://groups.google.com/group/ comp.lang.c ++ / MSG / 1d941c0f26ea0d81?PLI = 1

希望有人会更清楚地写出这个堆栈溢出来获取分数......

关注 Adam Rosenfield 的曲目,但修复了三元和逗号运算符的引用初始化问题:

bool outFileRequested = false;

std::ofstream realOutFile;
std::ostream & outFile = outFileRequested
    ? realOutFile.open("foo.txt", std::ios::out), realOutFile
    : std::cout;

outFile << "some witty remark";

(在VS中测试)

我认为Adam在正确的轨道上,但我认为你不能指定引用 - 你需要使用指针代替:

std::ofstream realOutFile;
std::ostream * poutFile;

if(outFileRequested)
{
    realOutFile.open("foo.txt", std::ios::out);
    poutFile = &realOutFile;
}
else
    poutFile = &std::cout;

然后你可以将引用定义为指针的值,但它不是全局的

std::ostream & outFile = *poutFile;

我不确定您是否可以将cout分配给ofstream类型的变量。 cout是ostream类型的对象(而cin的类型为istream),我不确定是否继承了另一个。因此,可能需要检查文件是否已存在/存在并创建适当的流类型是一种更好的方法。

这需要大约两个小时才能到达。基本上,我有一个运行测试套件的外部类。我发送一个委托来运行测试,所以为了能够访问输出,我需要发送输出流。我想我每次测试都可以做不同的流。无论如何,我想传入ofstream以便以后使用。

// Main code to create Test Suite Object
ofstream debugFile("debug.txt");
TestSuiteObject* myTestSuite = new TestSuiteObject(&debugFile);

// Test Suite Object
class TestSuiteObject: public Test::Suite
{
public:
 TestSuiteObject(std::ofstream* debug) : m_debug(*debug)
 {
  m_debug << "some witty remark" << std::endl;
  TEST_ADD(TestSuiteObject::test1);
  TEST_ADD(TestSuiteObject::test2);
  TEST_ADD(TestSuiteObject::test3);

 }

 void test1();
 void test2();
 void test3();

private:
 std::ofstream& m_debug;
};
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top