Writing into file from CommandIterationUpdate

Hey Guys, I am running ITK registration examples V4 (ImageRegistration4.cpp and the other on a roll), and I have a question:

How can I write the outputs in theCommandIterationUpdate class at each iteration step into a txt file?
The outputs are:
std::cout << optimizer->GetCurrentIteration() << " ";
std::cout << optimizer->GetValue() << " ";
std::cout << optimizer->GetCurrentPosition() << std::endl;

I tried creating and opening a file inside int main(), but the class CommandIterationUpdate does not recognize it.
Can you guys give-me a help?

Thanks!

Adapted from this IJ source code:

template<class TFilter>
class CommandIterationUpdate : public itk::Command
{
public:
    typedef CommandIterationUpdate   Self;
    typedef itk::Command             Superclass;
    typedef itk::SmartPointer<Self>  Pointer;
    itkNewMacro(Self);
protected:
    CommandIterationUpdate() {};
public:

    void Execute(itk::Object* caller, const itk::EventObject& event)
    {
        Execute((const itk::Object*) caller, event);
    }

    void Execute(const itk::Object* object, const itk::EventObject& event)
    {
        const TFilter* filter =
            dynamic_cast<const TFilter*>(object);
        if (typeid(event) != typeid(itk::IterationEvent))
        {
            return;
        }

        std::cout << "  Iteration " << filter->GetElapsedIterations()
            << " (of "
            << filter->GetMaximumNumberOfIterations()[filter->GetCurrentLevel()]
            << ").  ";
        std::cout << " Current convergence value = "
            << filter->GetCurrentConvergenceMeasurement()
            << " (threshold = " << filter->GetConvergenceThreshold()
            << ")" << std::endl;

        // here you could access outputs like this:
        filter->GetOutput(0);
        // I haven't tried this so it might not work
    }
};

int main()
{
    typedef itk::N4MRIBiasFieldCorrectionImageFilter<ImageType, MaskImageType,
        ImageType> CorrecterType;
...
    typedef CommandIterationUpdate<CorrecterType> CommandType;
    typename CommandType::Pointer observer = CommandType::New();
    correcter->AddObserver(itk::IterationEvent(), observer);
}

I see. But I still did not get how can I write such values into a .txt file.
I got how class CommandIterationUpdate but I do not know how can I say to that class to write such information at each iteration step.

That is more of a C++ question than ITK one.

Yes it sounds so. I’ll look around to see if I can find it.
Thanks!